{"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"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"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;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/\" \/>\n<meta property=\"og:site_name\" content=\"Ralf&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2007-05-08T14:40:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-01T15:25:28+00:00\" \/>\n<meta name=\"author\" content=\"Ralf Bergs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ralfbergs\" \/>\n<meta name=\"twitter:site\" content=\"@ralfbergs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ralf Bergs\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/\"},\"author\":{\"name\":\"Ralf Bergs\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#\\\/schema\\\/person\\\/354e37390b493c875f972bd313d29201\"},\"headline\":\"Monitor number of active connections to MySQL using Nagios\",\"datePublished\":\"2007-05-08T14:40:32+00:00\",\"dateModified\":\"2021-09-01T15:25:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/\"},\"wordCount\":154,\"publisher\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#\\\/schema\\\/person\\\/354e37390b493c875f972bd313d29201\"},\"keywords\":[\"mysql\",\"nagios\"],\"articleSection\":[\"Computers\",\"Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/\",\"url\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/\",\"name\":\"Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#website\"},\"datePublished\":\"2007-05-08T14:40:32+00:00\",\"dateModified\":\"2021-09-01T15:25:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/2007\\\/05\\\/08\\\/monitor-number-of-active-connections-to-mysql-using-nagios\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Monitor number of active connections to MySQL using Nagios\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/\",\"name\":\"Ralf's Blog\",\"description\":\"Just another WordPress weblog\",\"publisher\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#\\\/schema\\\/person\\\/354e37390b493c875f972bd313d29201\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/#\\\/schema\\\/person\\\/354e37390b493c875f972bd313d29201\",\"name\":\"Ralf Bergs\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/Ralf-Tower-2026-1024x1024.jpg\",\"url\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/Ralf-Tower-2026-1024x1024.jpg\",\"contentUrl\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/Ralf-Tower-2026-1024x1024.jpg\",\"width\":1024,\"height\":1024,\"caption\":\"Ralf Bergs\"},\"logo\":{\"@id\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/Ralf-Tower-2026-1024x1024.jpg\"},\"description\":\"Geek, computer guy, licensed and certified electrical and computer engineer, husband, best daddy.\",\"sameAs\":[\"https:\\\/\\\/bergs.biz\\\/\",\"https:\\\/\\\/linkedin.com\\\/in\\\/ralfbergs\\\/\",\"https:\\\/\\\/x.com\\\/ralfbergs\"],\"url\":\"https:\\\/\\\/bergs.biz\\\/blog\\\/author\\\/rabe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/","og_locale":"en_US","og_type":"article","og_title":"Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog","og_description":"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;]","og_url":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/","og_site_name":"Ralf&#039;s Blog","article_published_time":"2007-05-08T14:40:32+00:00","article_modified_time":"2021-09-01T15:25:28+00:00","author":"Ralf Bergs","twitter_card":"summary_large_image","twitter_creator":"@ralfbergs","twitter_site":"@ralfbergs","twitter_misc":{"Written by":"Ralf Bergs","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/#article","isPartOf":{"@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/"},"author":{"name":"Ralf Bergs","@id":"https:\/\/bergs.biz\/blog\/#\/schema\/person\/354e37390b493c875f972bd313d29201"},"headline":"Monitor number of active connections to MySQL using Nagios","datePublished":"2007-05-08T14:40:32+00:00","dateModified":"2021-09-01T15:25:28+00:00","mainEntityOfPage":{"@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/"},"wordCount":154,"publisher":{"@id":"https:\/\/bergs.biz\/blog\/#\/schema\/person\/354e37390b493c875f972bd313d29201"},"keywords":["mysql","nagios"],"articleSection":["Computers","Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/","url":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/","name":"Monitor number of active connections to MySQL using Nagios - Ralf&#039;s Blog","isPartOf":{"@id":"https:\/\/bergs.biz\/blog\/#website"},"datePublished":"2007-05-08T14:40:32+00:00","dateModified":"2021-09-01T15:25:28+00:00","breadcrumb":{"@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/bergs.biz\/blog\/2007\/05\/08\/monitor-number-of-active-connections-to-mysql-using-nagios\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bergs.biz\/blog\/"},{"@type":"ListItem","position":2,"name":"Monitor number of active connections to MySQL using Nagios"}]},{"@type":"WebSite","@id":"https:\/\/bergs.biz\/blog\/#website","url":"https:\/\/bergs.biz\/blog\/","name":"Ralf's Blog","description":"Just another WordPress weblog","publisher":{"@id":"https:\/\/bergs.biz\/blog\/#\/schema\/person\/354e37390b493c875f972bd313d29201"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bergs.biz\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/bergs.biz\/blog\/#\/schema\/person\/354e37390b493c875f972bd313d29201","name":"Ralf Bergs","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bergs.biz\/blog\/wp-content\/uploads\/2026\/04\/Ralf-Tower-2026-1024x1024.jpg","url":"https:\/\/bergs.biz\/blog\/wp-content\/uploads\/2026\/04\/Ralf-Tower-2026-1024x1024.jpg","contentUrl":"https:\/\/bergs.biz\/blog\/wp-content\/uploads\/2026\/04\/Ralf-Tower-2026-1024x1024.jpg","width":1024,"height":1024,"caption":"Ralf Bergs"},"logo":{"@id":"https:\/\/bergs.biz\/blog\/wp-content\/uploads\/2026\/04\/Ralf-Tower-2026-1024x1024.jpg"},"description":"Geek, computer guy, licensed and certified electrical and computer engineer, husband, best daddy.","sameAs":["https:\/\/bergs.biz\/","https:\/\/linkedin.com\/in\/ralfbergs\/","https:\/\/x.com\/ralfbergs"],"url":"https:\/\/bergs.biz\/blog\/author\/rabe\/"}]}},"_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}]}}