apg as bash function

# Generate a random password
#  $1 = number of chars, defaults to 12
function apg() {
  if [ "$1" = "" ]
  then
      len=8
  else
      len=`expr $1 / 2 + 1`
  fi
  # half of chars with special chars
  #     get random bits   | encode     | take line 2  | get char 2->
  var1=`head /dev/urandom | uuencode - | sed -n 2p    | cut -c2-$len`
  # rest of chars with base64
  #     get random bits   | encode base64 | take line 2 | get char 2->
  var2=`head /dev/urandom | uuencode -m - | sed -n 2p   | cut -c2-$len`
  echo "$var1$var2"
}

Suppress monit alerts upon dependency

Alert dependecy is useful for network equiptment, as connectivity often is dependent in a chain. Why get alert that your whole network is down, if the error is realy with your router/link/whatever? Making an alert dependency is possible with monit, but it’s a bit tricky.

This is because monit’s keyword then alert only make use of the depends keyword when the dependency is in state unmonitor. This makes it tricky to create an alert dependecy, as then unmonitor turns of the monitoring (you will not get notified when the error is resolved). A solution is to make another watcher which turns monitoring back on.

Here is an example where failed ICMP/ping turns of notification of failed TCP-services:

# used to turn of alerts with depends keyword
check host seljebuUp with address seljebu.no
    if failed icmp type echo count 3 with timeout 3 seconds then unmonitor

# turns on seljebuUp when error resolved, will also alert upon host up/down
check host seljebu with address seljebu.no
    if failed icmp type echo count 3 with timeout 3 seconds then exec "/bin/true"
    else if succeeded then exec "/usr/sbin/monit start seljebuUp"

# dependent on seljebuUp
check host seljebu-service with address seljebu.no
   if failed port 143 proto imap for 4 times within 5 cycles then alert
   if failed port 465 type tcpssl proto smtp for 4 times within 5 cycles then alert
   if failed url http://seljebu.no/ for 4 times within 5 cycles then alert
   depends seljebuUp

A little drawback here is double alerts upon outage and the extra action alerts (upon monit start seljebuUp). You can filter out the extra alerts by having $event in the subject (here, notifications for seljebuUp can be discarded). You could also turn of the action alerts by using set alert your@email.com but not on { action } in the global config (be careful, as this willl turn off all action alerts).