{"id":2814,"date":"2017-03-13T00:00:28","date_gmt":"2017-03-13T07:00:28","guid":{"rendered":"http:\/\/192.168.3.4\/?p=2814"},"modified":"2018-01-09T06:51:27","modified_gmt":"2018-01-09T14:51:27","slug":"cacti-and-custom-data-source-monitoring-part-2-of-2","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=2814","title":{"rendered":"Cacti and Custom Data Source Monitoring Part 2 of 2"},"content":{"rendered":"<p><strong>Custom Data Sources<\/strong><\/p>\n<p>Now that Cacti is up and running, let&#8217;s get some data from data sources that typically aren&#8217;t covered.\u00a0 The Raspberry Pi is unique in the sense that CPU temperature can be monitored using a program called vcgencmd.\u00a0 Great thing about this program is it&#8217;s already installed.\u00a0 Run the following command from the terminal.<\/p>\n<p>[bash]<br \/>\nvcgencmd measure_temp<br \/>\n[\/bash]<\/p>\n<p>You should get a reading back along the lines of &#8220;temp=42.0&#8217;C&#8221;.\u00a0 If you do, great.\u00a0 We&#8217;re going to need to create a script that will run on a schedule that will gather the temperature reading and place it into a text file.\u00a0 Let&#8217;s create our shell script using the following command.<\/p>\n<p>[bash]<br \/>\nsudo nano \/home\/local\/Tasks\/RPI_CPUTemp.sh<br \/>\n[\/bash]<\/p>\n<p>You can use your own text editor if you like, I prefer nano.\u00a0 Be sure to run as super user.\u00a0 We can change permissions and rights later using Webmin.\u00a0 Here is the code I used for my script.<\/p>\n<p>[bash]<br \/>\n#!\/bin\/sh<\/p>\n<p># Set variable for File<br \/>\nRPI_CPUTemp_File=&quot;\/home\/local\/Tasks\/RPI_CPUTemp.txt&quot;<\/p>\n<p># Run command and output results to that variable<br \/>\n\/opt\/vc\/bin\/vcgencmd measure_temp &amp;gt; &quot;$RPI_CPUTemp_File&quot;<\/p>\n<p># Get variable with results into new variable<br \/>\nRPI_CPUTemp_Var1=$(cat $RPI_CPUTemp_File)<\/p>\n<p># Remove extra characters out of variable and create new variable<br \/>\nRPI_CPUTemp_Var2=$(echo &quot;$RPI_CPUTemp_Var1&quot; | awk &#8216;{print substr($0,length-5,2)}&#8217;)<\/p>\n<p># Output the header and reading back into the File<br \/>\necho &quot;$RPI_CPUTemp_Var2&quot; &amp;gt; &quot;$RPI_CPUTemp_File&quot;<br \/>\n[\/bash]<\/p>\n<p>Once all the text is entered into the file, write it, save it, then close it.\u00a0 Now lets change the permissions to the file so it is level 0777 and set the owner to your MATE username and group.\u00a0 You&#8217;ll also want to be sure the path ownership is set to you MATE username and group.\u00a0 You can run the script from the terminal now by entering in this command<\/p>\n<p>[bash]<br \/>\n.\/RPI_CPUTemp.sh<br \/>\n[\/bash]<\/p>\n<p>This will create and write the readings to a file called RPI_CPUTemp.txt.\u00a0 Here is what should appear in that new file.<\/p>\n<p>42<\/p>\n<p>Really simple right!\u00a0 Now comes the part were we set a scheduled task to run this script every 5 minutes.\u00a0 I do this in Webmin because it&#8217;s a snap.\u00a0 You can look into using CRON, but I won&#8217;t cover that here.\u00a0 Webmin is so easy, I won&#8217;t even cover it.\u00a0 Suffice it to say, my task runs every 5 minutes.<\/p>\n<p>Now we&#8217;ll need to create a script that Cacti will run to gather that text file reading and do something useful with it.\u00a0 For that I created a perl script that does the leg work.\u00a0 Enter in this command at the terminal window.<\/p>\n<p>[bash]<br \/>\nsudo nano \/usr\/share\/cacti\/site\/scripts\/RPI_CPUTemp.pl<br \/>\n[\/bash]<\/p>\n<p>Here is the code for that perl script.<\/p>\n<p>[perl]<br \/>\n#!\/usr\/bin\/perl<\/p>\n<p>use strict;<br \/>\nuse warnings;<\/p>\n<p>open my $file, &#8216;&amp;lt;&#8217;, &quot;\/home\/local\/Tasks\/RPI_CPUTemp.txt&quot;;<br \/>\nmy $firstLine = &amp;lt;$file&amp;gt;;<br \/>\nprint $firstLine;<br \/>\nclose $file;<br \/>\n[\/perl]<\/p>\n<p>Once all the text is entered into the file, write it, save it, then close it.\u00a0 Now lets change the permissions to the file so it is level 0755.\u00a0 That&#8217;s it, now we&#8217;re ready to get inside Cacti and get this value polled and graphed.<\/p>\n<p>Login to Cacti and go to the Console tab and select Data Input Methods under the Collection Methods section, then select Add.\u00a0 Next enter in the name of the Data Input Method, &#8220;Localhost &#8211; CPU Temperature&#8221;.\u00a0 Choose Input Type as &#8220;Script\/Command&#8221;.\u00a0 Enter in the Input String &#8220;perl \/usr\/share\/cacti\/site\/scripts\/RPI_CPUTemp.pl&#8221; then click Create.<\/p>\n<p>Now Enter in the Output Fields section by clicking the Add link.\u00a0 In the Field [Output] entry put &#8220;rpitemp&#8221;, next enter in the Friendly Name &#8220;CPU Celsius&#8221; followed by the Create button.<\/p>\n<p>From there we continue on to the Templates section and choose Data Templates followed by clicking the Add link.\u00a0 Enter in the Data Templates Name &#8220;Localhost CPU Temperature&#8221; and the Data Source Name &#8220;|host_description| &#8211; CPU Temperature&#8221;.\u00a0 Next choose from the pulldown menu for the Data Input Method &#8220;Localhost &#8211; CPU Temperature&#8221;.\u00a0 In the Data Source Item enter in &#8220;rpitemp&#8221; for the Internal Data Source Name.\u00a0 I then set my Maximum Value at &#8220;50&#8221; followed by clicking the Create button.\u00a0 When I did that the Output Field auto populated with &#8220;rpitemp &#8211; CPU Celsius&#8221;.<\/p>\n<p>Next I went back to the Templates section and choose Graph Templates followed by clicking the Add link.\u00a0 Here I entered in the Template Name &#8220;CPU Temperature&#8221; and the Graph Template Name &#8220;|host_description| &#8211; CPU Temperature&#8221;.\u00a0 Then I set the Vertical Label to &#8220;Celsius&#8221; and clicked the Create button.<\/p>\n<p>Now I added the Graph Template Item by clicking the Add link.\u00a0 From the Data Source pulldown I selected &#8220;Localhost CPU Temperature &#8211; (rpitemp)&#8221;.\u00a0 Then I set the color from the pulldown &#8220;FF00FF&#8221;.\u00a0 Then I set the Graph Item Type to &#8220;AREA&#8221;.\u00a0 Afterwhich I set the Text Format to &#8220;CPU Celsius&#8221; and clicked Create.<\/p>\n<p>From there I then went to the Management section and selected Devices.\u00a0 In the list, I selected &#8220;Localhost&#8221;.\u00a0 In the Associated Graph Template portion, I selected &#8220;CPU Temperature&#8221; from the pulldown and clicked the Add button.\u00a0 Then up above, I clicked the &#8220;Create Graphs for this Host&#8221;.\u00a0 In the next windows, I choose &#8220;CPU Temperature&#8221; from the pulldown menu in the Create row, followed by the Create button.\u00a0 This takes you back to the last page, click the &#8220;Create Graphs for this Host&#8221; link again.\u00a0 Now check the box to the right of &#8220;Create: CPU Temperature&#8221;.<\/p>\n<p>Next I went to the Management section and choose Graph Management.\u00a0 I clicked on the entry &#8220;Localhost &#8211; CPU Temperature&#8221; and verified the entries were correct.\u00a0 I did the same verification under the Templates section for Graph Templates.<\/p>\n<p>I do realize that this has turned into a fantastic shit show.\u00a0 Do find comfort that our work is done.\u00a0 You should see a graph listed when you go to the Graphs tab.\u00a0 It&#8217;ll take time for &#8220;stuff&#8221; to populate the graph.\u00a0 To ensure all is working as expected, double check your scripts from the terminal window and echo out the results.<\/p>\n<p>These same steps can be used for other custom data input methods.\u00a0 Basically, any text file that has a number is a potential source for graph data in Cacti.\u00a0 The greatest challenge is creating a script that will poll, parse, and create the text file.\u00a0 If you can get a number, Cacti will graph it.<\/p>\n<p>Now I would like to cover Graph Template Items.\u00a0 These are useful features that can dress up the graphs with data that draws attention to conditions worth noticing.\u00a0 For instance, in our CPU temperature graph, it would be helpful to know how far below critical temp we are.\u00a0 We use Graph Template Items to plot those.\u00a0 Another example is that our reading is in Celsius.\u00a0 What if we needed that value to be in Fahrenheit instead.\u00a0 We used Graph Template Items, but also a sub feature called CDEF Functions to do that conversion.\u00a0 Yet another example would be to plot something when readings are unknown, to draw attention to a device being offline.\u00a0 All of these are handled by Graph Template Items.\u00a0 I&#8217;m going to step through these three examples next.<\/p>\n<p>First, lets dive into the CDEF Functions because this will be a fundamental requirement for use later on.\u00a0 From the Console click on Graph Management under Management.\u00a0 This will expand a subsection list.\u00a0 Now we want to click on the link &#8220;&#8212;CDEFs&#8221;.\u00a0 You&#8217;ll see some preset math functions already in there.\u00a0 We want to add a CDEF for converting temperature from Celsius to Fahrenheit, click the Add link.<\/p>\n<p>Enter in the CDEF name &#8220;Celsius to Fahrenheit&#8221; and click Create.\u00a0 Now we will have a CDEF Items list were we can add our functions.\u00a0 Here is the list of CDEF Item Types and CDEF Item Values to enter in for this function.<\/p>\n<p>Custom String: 1.3<br \/>\nSpecial Data Source: CURRENT_DATA_SOURCE<br \/>\nOperator: *<br \/>\nCustom String: 32<br \/>\nOperator: +<\/p>\n<p>You will want to be sure that your CDEF list is in the correct order.\u00a0 Cacti performs these functions in a set way.\u00a0 If you order is wrong, your results will be wrong.\u00a0 You should see your items grouped together above, here is what it should look like.<\/p>\n<p>cdef=1.3,CURRENT_DATA_SOURCE,*,32,+<\/p>\n<p>If all looks good, click the Save button.\u00a0 Now we are done with CDEF functions and ready to put our focus on Graph Template Items.\u00a0 Lets start by adding a line that will indicate our upper acceptable temperature limit for our CPU.\u00a0 According to online docs, that value seems to be 80 degrees Celsius.\u00a0 I&#8217;m going to add a upper threshold line at 75 degrees Celsius.<\/p>\n<p>Click Graph Templates from the Console Templates section and choose the CPU Temperature Item.\u00a0 Next click Add on the Graph Template Items.\u00a0 Here we want to set our values as such.<\/p>\n<p>Data Source: Localhost CPU Temperature &#8211; (rpitemp)<br \/>\nColor: B90094<br \/>\nOpacity\/Alpha Channel: 100%<br \/>\nGraph Item Type: HRULE<br \/>\nConsolidated Function: AVERAGE<br \/>\nCDEF Function: None<br \/>\nValue: 75<\/p>\n<p>Click Save and Save again to apply to the Graph Template.\u00a0 Do note that the auto scaling of the graph may not make the threshold line appear, but be rest assured it is there.\u00a0 If you want to see it, uncheck the Auto Scale check box in the Graph Template Items window.\u00a0 You can also set Lower and Upper Limits by checking the boxes for Upper Limit and Lower Limit and entering in the values.\u00a0 You&#8217;ll have a Supplemental Graph Template Data entry appear when you go back into Graph Management and select Localhost CPU Temperature.\u00a0 I set my upper limit to 85 and my lower to 35.<\/p>\n<p>Now lets add a plot for unknown values in our graph for readings that fail to be made.\u00a0 This is useful for plotting ping responses from online hosts, not so much for CPU temp.\u00a0 However, the same steps apply.\u00a0 First we&#8217;ll need to create a CDEF for it.\u00a0 Enter in these settings for a new CDEF called Unknown.\u00a0 Here is the list of CDEF Item Types and CDEF Item Values to enter in for this function.<\/p>\n<p>Special Data Source: CURRENT_DATA_SOURCE<br \/>\nFunction: UN<br \/>\nFunction: INF<br \/>\nFunction: UNKN<br \/>\nFunction: IF<\/p>\n<p>You should see your items grouped together above, here is what it should look like.<\/p>\n<p>cdef=CURRENT_DATA_SOURCE,UN,INF,UNKN,IF<\/p>\n<p>Now, click Graph Templates from the Console Templates section and choose the CPU Temperature Item.\u00a0 Next click Add on the Graph Template Items.\u00a0 Here we want to set our values as such.<\/p>\n<p>Data Source: Localhost CPU Temperature &#8211; (rpitemp)<br \/>\nColor: FFC3C0<br \/>\nOpacity\/Alpha Channel: 100%<br \/>\nGraph Item Type: AREA<br \/>\nConsolidated Function: AVERAGE<br \/>\nCDEF Function: Unknown<\/p>\n<p>I typically place this Graph Item on the top of my list in the Graph Template Items list.\u00a0 Viewing the graph will now display a pink area when values are not plotted.\u00a0 Plotting our temperature in Fahrenheit follows the same basic steps as plotting unknown values.\u00a0 The only difference would be choosing a different color, graph item type, and CDEF function.\u00a0 Here is an example of what we would enter in.<\/p>\n<p>Data Source: Localhost CPU Temperature &#8211; (rpitemp)<br \/>\nColor: 157419<br \/>\nOpacity\/Alpha Channel: 100%<br \/>\nGraph Item Type: LINE2<br \/>\nConsolidated Function: AVERAGE<br \/>\nCDEF Function: Celsius to Fahrenheit<\/p>\n<p>That should cover most of the custom data source and graph functions you could use in Cacti.\u00a0 I tried to cover as much in a single post, but the steps involved are far too many.\u00a0 This will be fundamental as I continue on with later posts.\u00a0 There I will be gathering data from sensors and conditions to be plotted in Cacti.\u00a0 I&#8217;ll also cover some other features in Cacti not covered yet.\u00a0 Thank you for hanging in there and I hope that you have found this useful information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Custom Data Sources Now that Cacti is up and running, let&#8217;s get some data from data sources that typically aren&#8217;t covered.\u00a0 The Raspberry Pi is unique in the sense that CPU temperature can be monitored using a program called vcgencmd.\u00a0 Great thing about this program is it&#8217;s already installed.\u00a0 Run the following command from the terminal. [bash] vcgencmd measure_temp [\/bash] You should get a reading back along the lines of &#8220;temp=42.0&#8217;C&#8221;.\u00a0 If you do, great.\u00a0 We&#8217;re going to need to&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=2814\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-2814","post","type-post","status-publish","format-standard","hentry","category-data-mining"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/2814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2814"}],"version-history":[{"count":3,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/2814\/revisions"}],"predecessor-version":[{"id":2818,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/2814\/revisions\/2818"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}