Tuesday 16 August 2011

advanced BASH scripting

Simple things:
To assign a variable to an array:

array_config_timestamp[$i]="${array_config[$i]:0:16}"
Breaking this down, $i = index
0:16 is the substring position within the string

(( i++ ))
adds  1 to the variable i
max_updates=$(( $j-1 ))
j =j-1

Converting seconds to HH:MM:SS

hours=$(( ${array_error_time[$i]} / 3600 ))
seconds=$(( ${array_error_time[$i]} % 3600 ))
minutes=$(( $seconds / 60 ))
seconds=$(( $seconds % 60 ))

So basically (( )) interprets the expression within, % is the basic arithmetic function mod


 array_error_updatei[$error_index]=`/usr/bin/expr index "${array_index_name[$j]:10}" _`
This searches for the first occurence of _ within the array array_index_name with index j from position 10

printf "%s %d\x68%d\x6d%d\x73" ${array_error[$i]:9} $hours $minutes $seconds
prints formatted text ${array_error[$i]:9} and HH:MM:SS as 1h 2m 3s

Check_mk useful locations

/var/lib/check_mk/autochecks - Where check_mk stores the cached inventory files
/usr/share/check_mk/checks/ - Where the actual server checks are stored
/usr/local/share/pnp4nagios/templates - where the performance graph templates are stored
/usr/share/check_mk/web/plugins/perfometer - where the perfometer templates are stored.

Sunday 7 August 2011

Programming alfred applescript extensions for powerpack

this is a basic conversion program written on applescript for inclusion into alfred.

call up alfred, and type
convert 30 ft to m
or
convert 30 m to ft

this can be easily extended by adding your own variables.

on alfred_script(q)
    --display dialog q
       -- set for when running from applescript
    -- set q to "30 ft to m"
    set Feet2Meters to 0.3048
    set Meters2Feet to 3.2808399
   
    --find first occurance of unit
    set max_args to number of q
    if max_args = 1 then
        set number1 to ( q)
        if number1 = "?"
            display dialog "Help\nConversion ft to m"
            exit
        end if
    else
        set number1 to (word 1 of q)
        set unit1 to (word 2 of q)
        --set number2 to (word 3 of q)
        set unit2 to (word 4 of q)
    end if

    -- display dialog "1: " & number1 & ", 2:" & unit1 & ", 3:" & unit2
   
    if unit1 = "ft" then
        if unit2 ="m" then
            set conversion to Feet2Meters
        end if
    end if
    if unit1 = "m" then
        if unit2 = "ft" then
            set conversion to Meters2Feet
        end if
    end if
   
    set final to number1 * conversion
    -- display dialog final
    display dialog "Conversion: " & number1 & " " & unit1 & " is " & final & " " & unit2
end alfred_script