{"id":39000,"date":"2016-08-26T14:30:51","date_gmt":"2016-08-26T09:00:51","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=39000"},"modified":"2024-01-02T18:32:39","modified_gmt":"2024-01-02T13:02:39","slug":"create-a-custom-metric-in-zabbix","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-a-custom-metric-in-zabbix\/","title":{"rendered":"Create a Custom Metric in Zabbix"},"content":{"rendered":"<p><span style=\"color: #000000;\">In our <a title=\"Getting Started with Zabbix\" href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-zabbix\/\" target=\"_blank\" rel=\"noopener\">recent blog\u00a0post<\/a><span style=\"color: #ff00ff;\">,<\/span> we have covered installation of Zabbix Server, and agent, and how to configure a host for monitoring. Today we are going to dive into how to create a custom metric in Zabbix. Finally, we&#8217;ll create a\u00a0custom metric for monitoring Nginx server.<\/span><\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Zabbix_logo.png\" alt=\"\" \/><\/p>\n<h2><span style=\"color: #000000;\">So, let&#8217;s get started.<\/span><\/h2>\n<p><span style=\"color: #000000;\">Assuming that you have already configured Zabbix Server and Zabbix agent(in Nginx server). If not, please follow our previous <a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-zabbix\/\" target=\"_blank\" rel=\"noopener\">blog on Zabbix<\/a>.<\/span><\/p>\n<p><span style=\"color: #000000;\">To configure custom metric, Zabbix provides something called userparameter. This userparameter is defined in the Zabbix agent configuration file.<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax of UserParameter is as follows:<\/span><\/p>\n<p>[js]<\/p>\n<p>UserParameter=&lt;key&gt;,&lt;command&gt;<\/p>\n<p>[\/js]<\/p>\n<p><span style=\"color: #000000;\">In the above syntax, the key is the <b>item key <\/b>used to retrieve data from Zabbix agent and output generated by the command is the data retrieved from Zabbix agent. Note that default item key provided by Zabbix server cannot be used in userparameter.<\/span><\/p>\n<p><span style=\"color: #000000;\">So, let&#8217;s start with Nginx monitoring.<\/span><\/p>\n<p><span style=\"color: #000000;\">Basic status information about Nginx can be accessed via stub module provided by Nginx. We will use this module to monitor our Nginx server.<\/span><\/p>\n<p><span style=\"color: #000000;\">Nginx must be compiled with http-stub module. Nginx installed using apt-get by default contains http-stub module. To check if Nginx contains stub module, run the following command:<\/span><\/p>\n<p>[js]<\/p>\n<p>nginx -V 2&gt;&amp;1 | grep -o with-http_stub_status_module<\/p>\n<p>[\/js]<\/p>\n<p><span style=\"color: #000000;\">We will need to add the following configuration at Nginx site:<\/span><\/p>\n<p>[js]<\/p>\n<p>location \/nginx_status <\/p>\n<p>{<\/p>\n<p> stub_status on; <\/p>\n<p> access_log off;<\/p>\n<p> allow 127.0.0.1; <\/p>\n<p> deny all; <\/p>\n<p>}<\/p>\n<p>[\/js]<\/p>\n<p><span style=\"color: #000000;\">After adding the following configuration, restart Nginx. Now, that we have stub module enabled, basic information about Nginx can be accessed on visiting localhost\/nginx_status from the same server:<\/span><\/p>\n<p>[js]<\/p>\n<p>curl localhost\/nginx_status<\/p>\n<p>Active connections: 32<br \/>\nserver accepts handled requests<br \/>\n54 54 83<br \/>\nReading: 6 Writing: 2 Waiting: 12<\/p>\n<p>[\/js]<\/p>\n<p><span style=\"color: #000000;\">Now that we have configured Nginx to show stats, it&#8217;s time to create the custom metric for the same.<\/span><\/p>\n<p><span style=\"color: #000000;\">For our Zabbix agent to send Nginx monitoring data to Zabbix server, it needs to have userparameter defined in its configuration file. So, create a new configuration file for Zabbix agent:<\/span><\/p>\n<p>[shell]<\/p>\n<p>vi \/etc\/zabbix\/zabbix_agentd.conf.d\/userparameter_nginx.conf<\/p>\n<p>UserParameter=nginx.active.conn,curl localhost\/nginx_status 2&gt;\/dev\/null | head -1 | cut -d &quot;:&quot; -f 2 | | cut -d &quot; &quot; -f 2<\/p>\n<p>UserParameter=nginx.server_accept,curl localhost\/nginx_status 2&gt;\/dev\/null | head -3 | tail -1 | cut -d &quot; &quot; -f 2 <\/p>\n<p>UserParameter=nginx.server_handled,curl localhost\/nginx_status 2&gt;\/dev\/null | head -3 | tail -1 | cut -d &quot; &quot; -f 3 <\/p>\n<p>UserParameter=nginx.server_request,curl localhost\/nginx_status 2&gt;\/dev\/null | head -3 | tail -1 | cut -d &quot; &quot; -f 4 <\/p>\n<p>UserParameter=nginx.reading,curl localhost\/nginx_status 2&gt;\/dev\/null | tail -1 | cut -d &quot;:&quot; -f 2 | cut -d &quot; &quot; -f 2 <\/p>\n<p>UserParameter=nginx.writing,curl localhost\/nginx_status 2&gt;\/dev\/null | tail -1 | cut -d &quot;:&quot; -f 3 | cut -d &quot; &quot; -f 2 <\/p>\n<p>UserParameter=nginx.waiting,curl localhost\/nginx_status 2&gt;\/dev\/null | tail -1 | cut -d &quot;:&quot; -f 4 | cut -d &quot; &quot; -f 2<\/p>\n<p>[\/shell]<\/p>\n<p><span style=\"color: #000000;\">Now that we are done configuring Zabbix agent, let&#8217;s restart the service to apply changes. With this, our agent is ready to send custom metric to the Zabbix server. <\/span><\/p>\n<p><span style=\"color: #000000;\">Now, it&#8217;s time to configure the host on Zabbix Server using User Interface. Open Zabbix Server UI and navigate to Configuration-&gt; hosts and click on create host:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39502\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_16_35852598924.jpg\" alt=\"imageedit_16_3585259892\" width=\"941\" height=\"500\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_16_35852598924.jpg 941w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_16_35852598924-300x159.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_16_35852598924-624x331.jpg 624w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<p><span style=\"color: #000000;\">Provide hostname, host group and IP address of the web server and click on add host to create a new host on Zabbix Server:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39506\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_18_68397135621.jpg\" alt=\"imageedit_18_6839713562\" width=\"941\" height=\"195\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_18_68397135621.jpg 941w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_18_68397135621-300x62.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_18_68397135621-624x129.jpg 624w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<p><span style=\"color: #000000;\">Now, it&#8217;s time create custom items to get Nginx monitoring data. To create a new item, navigate to host item and click on create item:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39508\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_21_72004641961.jpg\" alt=\"imageedit_21_7200464196\" width=\"941\" height=\"499\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_21_72004641961.jpg 941w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_21_72004641961-300x159.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_21_72004641961-624x330.jpg 624w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<p><span style=\"color: #000000;\">Provide name and key to create a new item. The key should be the same as defined in the userparameter in Zabbix agent(for example:\/ nginx.active_conn).<\/span><\/p>\n<p><span style=\"color: #000000;\">Likewise, create items for all the userparameter configured at Zabbix agent.<\/span><\/p>\n<p><span style=\"color: #000000;\">Now that everything is configured, navigate to Monitoring-&gt;Latest Data and select the host (that you have configured) in the filter section:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-39509\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_24_88183750431.jpg\" alt=\"imageedit_24_8818375043\" width=\"941\" height=\"423\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_24_88183750431.jpg 941w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_24_88183750431-300x134.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/imageedit_24_88183750431-624x280.jpg 624w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<p><span style=\"color: #000000;\">The above image shows the latest value gathered by all the items.<\/span><\/p>\n<p><span style=\"color: #000000;\">There you have it, another amazing feature of Zabbix.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our recent blog\u00a0post, we have covered installation of Zabbix Server, and agent, and how to configure a host for monitoring. Today we are going to dive into how to create a custom metric in Zabbix. Finally, we&#8217;ll create a\u00a0custom metric for monitoring Nginx server. So, let&#8217;s get started. Assuming that you have already configured [&hellip;]<\/p>\n","protected":false},"author":918,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"link","meta":{"iawp_total_views":92},"categories":[1174,2348,1],"tags":[1892,3897,2380,3591,3704,3706,3705],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39000"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/918"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=39000"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39000\/revisions"}],"predecessor-version":[{"id":59921,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39000\/revisions\/59921"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=39000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=39000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=39000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}