Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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="    check_command $cc";
    cat > /path/to/nagios/configs/${hn}-${sn}.cfg <<EOF
define service {
    use $sn
    host_name $hn

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

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
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

Your Answer


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.