Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question is only about creating services, i.e. we already know how to write Nagios plugins and set them up as services.

I'd like to write a Python script to read each row of a MySQL database table and for each row create a service. We prefer to see a green/red light for each row rather than have a single service that checks the entire table. The number of rows in this database table means it would take a long time to enter each service manually.

I've looked into scripts like radar. This however operates on passive checks, which we don't need. We simply have a list of services we'd like to create as active checks.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Just iterate over the table, write out the necessary info into a cfg file, put this into a location that is loaded by the main nagios config file and reload the nagios config. Depending on what you need/want to check, write an appropriate plugin. Done. Can be done real fast.

share|improve this answer
add comment

Can be done in bash, even. Broadly:

mysql -Ne 'SELECT service_name,'#',IFNULL(check_command,"-"),'#',hostname FROM table_of_wanted_nagios_monitors' |
  while IFS='#' read sn cc hn; do
    # strip extra spaces (due to IFS) from $cc for testing only
    scc=$(echo "$cc" | sed 's/ //g')
    if [ "x$scc" == "x-" -o "x$scc" == "x" ]; then
      cc="";
    else
      cc="    check_command $cc";
    fi;
    cat > /path/to/nagios/configs/${hn}-${sn}.cfg <<EOF
define service {
    use $sn
    host_name $hn
$cc
}
EOF
done

We use something like this (albeit written in a different language, and with quite a bit more complexity) that queries some internal APIs to populate our Nagios. Works like a champ.

share|improve this answer
add comment

There are many configuration add-ons that do exactly what you describe.

You may be able to use one "off the shelf", adapt one to your needs, or just steal some code from one of them.

share|improve this answer
1  
that listing seems a bit outdated, since they feature lilac which has been dead for some years now. you might also try the most popular and free around, such as nagiosql/nconf/lconf/adagios or use some automation tool like puppet manifests or chef cookbooks. –  dnsmichi Jun 18 '13 at 21:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.