{"id":43,"date":"2007-05-08T15:40:32","date_gmt":"2007-05-08T14:40:32","guid":{"rendered":"http:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/"},"modified":"2021-09-01T17:25:28","modified_gmt":"2021-09-01T15:25:28","slug":"monitor-number-of-active-connections-to-mysql-using-nagios","status":"publish","type":"post","link":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/","title":{"rendered":"Monitor number of active connections to MySQL using Nagios"},"content":{"rendered":"<p>I need to monitor whether the number of active connections against a MySQL server is within a &#8220;reasonable&#8221; range.<\/p>\n<p>For all my monitoring needs, I use <a href=\"http:\/\/nagios.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Nagios<\/a>. Of course, Nagios offers a MySQL monitoring plugin, however, it does not suit my needs. It only allows you to perform queries on tables, which doesn&#8217;t allow me to retrieve the number of current connections to the database server (or does it?!).<\/p>\n<p><!--more-->Therefore, I rolled my own plugin. Here it is:<\/p>\n<pre>#!\/usr\/bin\/perl -w\r\n\r\nuse strict;\r\nuse Getopt::Std;\r\nuse lib \"\/usr\/lib\/nagios\/plugins\";\r\nuse utils qw(%ERRORS);\r\n\r\nuse vars qw\/ %opt \/;\r\nsub debug($);\r\n\r\ngetopts('c:dhp:u:w:', \\%opt);\r\n\r\nif (exists $opt{h}) {\r\n    usage();\r\n    exit(0);\r\n}\r\n\r\nmy $debug = 0;\r\nif (exists $opt{d}) {\r\n    print \"Enabling debug mode...\\n\";\r\n    $debug = 1;\r\n}\r\n\r\nmy $critical_threshold = 10;\r\nif (exists $opt{c}) {\r\n    $critical_threshold = $opt{c};\r\n}\r\ndebug(\"\\$critical_threshold=$critical_threshold\\n\");\r\n\r\nmy $warn_threshold = 5;\r\nif (exists $opt{w}) {\r\n    $warn_threshold = $opt{w};\r\n}\r\ndebug(\"\\$warn_threshold=$warn_threshold\\n\");\r\n\r\nmy $username = \"monitoring\";\r\nif (exists $opt{u}) {\r\n    $username = $opt{u};\r\n}\r\n\r\nmy $password = \"monitoring\";\r\nif (exists $opt{p}) {\r\n    $username = $opt{p};\r\n}\r\n\r\nmy $query_output = `\/bin\/echo \"SHOW GLOBAL STATUS LIKE 'Threads_connected';\" \\| \/usr\/bin\/mysql -u $username -p$password \\| \/bin\/grep \"Threads_connected\"`;\r\ndebug(\"\\$query_output=\\\"$query_output\\\"\\n\");\r\n$query_output =~ \/^Threads_connected\\s+(\\d+)\\s+$\/;\r\nmy $threads_connected = $1;\r\ndebug(\"\\$threads_connected=$threads_connected\\n\");\r\n\r\nif ($threads_connected &gt; $critical_threshold) {\r\n    print \"Critical: $threads_connected active connections\\n\";\r\n    exit $ERRORS{'CRITICAL'}\r\n} elsif ($threads_connected &gt; $warn_threshold) {\r\n    print \"Warning: $threads_connected active connections\\n\";\r\n    exit $ERRORS{'WARNING'}\r\n} else {\r\n    print \"OK: $threads_connected active connections\\n\";\r\n    exit $ERRORS{'OK'}\r\n}\r\n\r\n###########################################################################\r\n\r\nsub usage {\r\n    if (@_ == 1) {\r\n\tprint \"$0: $_[0].\\n\";\r\n    }\r\n    print &lt; &lt; \"EOF\";\r\nUsage: $0 [options]\r\n  -c THRESHOLD\r\n     critical threshold for number of active connections (default: 10)\r\n  -d\r\n     enable debug mode (mutually exclusive to -q)\r\n  -h\r\n     display usage information\r\n  -p PASSWORD\r\n     The password to use when connecting to the server.\r\n  -u USERNAME\r\n     The MySQL username to use when connecting to the server.\r\n  -w THRESHOLD\r\n     Warning threshold for number of active connections (default: 5)\r\nEOF\r\n}\r\n\r\nsub debug($) {\r\n    if ($debug) {\r\n\tprint STDERR $_[0];\r\n    }\r\n}<\/pre>\n<p>Admittedly it&#8217;s very simple, but it works. \ud83d\ude42<\/p>\n<p><strong>Update:<\/strong> There seems to be a bug in WordPress that &#8220;gobbles&#8221; characters from out of the &#8220;pre&#8221; section above. I don&#8217;t know how to make WordPress keep all characters I paste into the &#8220;pre&#8221; section and render them correctly. Therefore I have placed the script in a static section of my homepage. Download the file <a title=\"check_mysql_conn.pl\" href=\"http:\/\/bergs.biz\/blog\/static\/check_mysql_conn.pl\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I need to monitor whether the number of active connections against a MySQL server is within a &#8220;reasonable&#8221; range. For all my monitoring needs, I use Nagios. Of course, Nagios offers a MySQL monitoring plugin, however, it does not suit my needs. It only allows you to perform queries on tables, which doesn&#8217;t allow me [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,7],"tags":[22,23],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-computers","category-development","tag-mysql","tag-nagios"],"_links":{"self":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":1,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":1637,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions\/1637"}],"wp:attachment":[{"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bergs.biz\/blog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}