{"id":1824,"date":"2015-02-20T00:00:04","date_gmt":"2015-02-20T08:00:04","guid":{"rendered":"http:\/\/192.168.3.4\/?p=1824"},"modified":"2018-01-09T06:49:49","modified_gmt":"2018-01-09T14:49:49","slug":"remote-and-local-rpi-sensors-in-cacti","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=1824","title":{"rendered":"Remote and Local RPi Sensors in Cacti"},"content":{"rendered":"<p><strong>Introduction &#8211; how&#8217;s it going<br \/>\n<\/strong><\/p>\n<p>This post will cover the logging of data in Cacti using non SNMP\/WMI sources.\u00a0 We&#8217;ll gather <a href=\"https:\/\/github.com\/raspberrypilearning\/temperature-log\/blob\/master\/worksheet.md\" target=\"_blank\">local RPi<\/a> information and plot it in Cacti.\u00a0 Then we will go through the procedures to gather data from the internet and process it into Cacti.\u00a0 This is an expansion of the <a href=\"http:\/\/www.mikeslab.net\/?p=172\" target=\"_blank\">NOAA XML file processing<\/a> done in an earlier post.<\/p>\n<p><strong>Purpose &#8211; having a reference can give you an idea of were you&#8217;re going<br \/>\n<\/strong><\/p>\n<p>Cacti is a multi purpose network device tracking tool.\u00a0 In addition to that, it can graph data plots from other sources.<\/p>\n<p><strong>Details &#8211; getting there isn&#8217;t always as easy as it seems<br \/>\n<\/strong><\/p>\n<p>Lets start with setting up our Cacti interface.\u00a0 This was done on my earlier post, but I want to revisit it and make it more clear.\u00a0 We&#8217;ll be adding 4 sensors to our Cacti interface.\u00a0 These are 3 NOAA readings and 1 reading from the RPi chipset.\u00a0 Start by logging into the Cacti interface.<\/p>\n<p>A &#8211; The first step is to define the Data Input Method.<\/p>\n<ol>\n<li>From the Console, click &#8220;Data Input Methods&#8221; from the Collection Methods section.<\/li>\n<li>Click the Add link.<\/li>\n<li>Enter a meaningful name for the DIM, ie. &#8220;<span style=\"text-decoration: underline;\">RPi Chipset Temp<\/span>&#8220;.<\/li>\n<li>Select &#8220;Script\/Command&#8221; from the Input Type pull down list.<\/li>\n<li>Enter in the script that will poll and format the results for Cacti, ie. &#8220;<span style=\"text-decoration: underline;\">\/var\/www\/cacti\/scripts\/rpitemp.py<\/span>&#8220;.<\/li>\n<li>Click save and this will add more fields to enter.<\/li>\n<li>Click add in the Output Fields list.<\/li>\n<li>Enter the field output, this has to match the script results deliminator in step 6, ie. &#8220;<span style=\"text-decoration: underline;\">rpitemp<\/span>&#8220;.<\/li>\n<li>Click save to set the Output Fields option.<\/li>\n<li>Click save to set the DIM settings.<\/li>\n<\/ol>\n<p>B &#8211; The second step is to define the Data Sources.<\/p>\n<ol>\n<li>From the Console, click &#8220;Data Sources&#8221; under the Management section.<\/li>\n<li>Click the Add link.<\/li>\n<li>From the Host list, select the local machine.\u00a0 It should have the loopback IP listed, ie. 127.0.0.1.<\/li>\n<li>Click the Create button.<\/li>\n<li>Now more fields should be available.<\/li>\n<li>Enter in the Data Source Name, ie. &#8220;<span style=\"text-decoration: underline;\">RPi Chipset Temp<\/span>&#8220;.<\/li>\n<li>From the pull down in the Data Input Method section choose the correct DIM, ie. &#8220;<span style=\"text-decoration: underline;\">RPi Chipset Temp<\/span>&#8220;.<\/li>\n<li>In the Data Source Item section, enter in the Internal Data Source Name.\u00a0 This has to match the item entered in step 10 and 6, ie. &#8220;<span style=\"text-decoration: underline;\">rpitemp<\/span>&#8220;.<\/li>\n<li>Lastly, enter in the Minimum and Maximum values.<\/li>\n<\/ol>\n<p>C &#8211; Now the third step is to create the Graph Template that the readings will report on.<\/p>\n<ol>\n<li>From the Console, click &#8220;Graph Management&#8221; under the Management section.<\/li>\n<li>Click the Add link.<\/li>\n<li>From the Host list, select the local machine.\u00a0 It should have the loopback IP listed, ie. 127.0.0.1.<\/li>\n<li>Click the Create button.<\/li>\n<li>Now more fields should be available.<\/li>\n<li>Enter in the Title under Graph Configuration, ie. &#8220;<span style=\"text-decoration: underline;\">NOAA Seattle Temp<\/span>&#8220;.<\/li>\n<li>From the Vertical Label enter in the unit of measure, ie. &#8220;<span style=\"text-decoration: underline;\">fahrenheit<\/span>&#8220;.<\/li>\n<li>Now we&#8217;re ready to add Graph Items up above, click the Add link.<\/li>\n<li>In the Graph Items section, use the Data Source pull down to select the correct DS, ie. &#8220;<span style=\"text-decoration: underline;\">NOAASeaTemp (noaaseatemp)<\/span>&#8220;.<\/li>\n<li>Choose your color from the pull down, ie. &#8220;<span style=\"text-decoration: underline;\">6EA100<\/span>&#8220;.<\/li>\n<li>Change your Graph Item Type to &#8220;<span style=\"text-decoration: underline;\">AREA<\/span>&#8220;.<\/li>\n<li>In the Text Format field, enter in a description of the reading, ie. &#8220;<span style=\"text-decoration: underline;\">Tempurature<\/span>&#8220;.<\/li>\n<li>Click save to commit and now the graph should appear in the Graph Template Selection window, under Graph Items.<\/li>\n<li>Click save again and now you are done.<\/li>\n<\/ol>\n<p>Go through these steps for each of the 4 sensors.\u00a0 Don&#8217;t worry too much about the script in step A5, we&#8217;ll do that next.\u00a0 This main thing up to this point is getting Cacti setup.\u00a0 Since we&#8217;re on the subject, lets start checking our scripts.\u00a0 Since most of the leg work has been done on the NOAA scraped data, we&#8217;ll start with that.<\/p>\n<p>First off, I had run into a some issues when I setup the Cacti data set.\u00a0 It was along the lines of the error the folks at NASA had made with imperial versus metric, which resulted in a <a href=\"http:\/\/mars.jpl.nasa.gov\/msp98\/news\/mco990930.html\" target=\"_blank\">lost mission<\/a>.\u00a0 I set my max value to 50 degrees, thinking it we would never get a day hotter than 122F.\u00a0 Problem was my readings were in Fahrenheit.\u00a0 The only way to correct this was to change the max value, delete the old rrd data set, and recreate it.<\/p>\n<p>Next up are the scripts that scrape the readings from NOAA.\u00a0 I recycled the scripts and didn&#8217;t change the variable definitions.\u00a0 It caused some overlap which resulted in erroneous values.\u00a0 When I created unique variable definitions, it worked like a charm.\u00a0 It&#8217;s the easy stuff that will gang up on you and throw you off the mountain.<\/p>\n<p>Finally, my last botch was connecting an IDE cable to the GPIO pins while the system was hot.\u00a0 Really, how brain dead is that?!\u00a0 Needless to say, the RPi became bricked.\u00a0 I had hoped that it was a thermal fuse that would reset, but time wasn&#8217;t a healer.\u00a0 So I got to put my backup\/restore procedures to the test.\u00a0 Now I&#8217;m running everything on the latest RPi and I think I&#8217;m a better person for it.<\/p>\n<p>So here is the script I&#8217;m using to scrape readings from NOAA with the Cacti formatted output.<\/p>\n<pre>\r\n<code>\r\n#!\/usr\/bin\/python\r\n\r\nfrom lxml import etree\r\nimport urllib2\r\nimport os\r\n\r\n<span style=\"color:red\">tmp<\/span>url = 'http:\/\/w1.weather.gov\/xml\/current_obs\/KSEA.xml'\r\n<span style=\"color:red\">tmp<\/span>fp = urllib2.urlopen(<span style=\"color:red\">tmp<\/span>url)\r\n<span style=\"color:red\">tmp<\/span>doc = etree.parse(<span style=\"color:red\">tmp<\/span>fp)\r\n<span style=\"color:red\">tmp<\/span>fp.close()\r\n\r\n<span style=\"color:red\">tmp<\/span>temp = <span style=\"color:red\">tmp<\/span>doc.xpath(\"\/\/current_observation\/temp_f\")[0].text\r\n<span style=\"color:red\">tmp<\/span>temp = <span style=\"color:red\">tmp<\/span>temp[:-2]\r\n\r\nfile = open('\/var\/www\/cacti\/scripts\/noaaseatemp.txt' , 'w')\r\n<span style=\"color:red\">tmp<\/span>keyval = \"noaaseatemp:\"\r\n<span style=\"color:red\">tmp<\/span>staticval = str(<span style=\"color:red\">tmp<\/span>keyval)\r\n<span style=\"color:red\">tmp<\/span>value = str(<span style=\"color:red\">tmp<\/span>temp)\r\nfile.write(<span style=\"color:red\">tmp<\/span>staticval)\r\nfile.write(<span style=\"color:red\">tmp<\/span>value)\r\nfile.close()\r\n\r\nfile = open('\/var\/www\/cacti\/scripts\/noaaseatemp.txt' , 'r')\r\n<span style=\"color:red\">tmp<\/span>variable = file.read()\r\nfile.close()\r\nprint <span style=\"color:red\">tmp<\/span>variable\r\n<\/code>\r\n<\/pre>\n<p>The last one we&#8217;ll look at is the RPi BCM2835 system on a chip (SoC) temperature readings.\u00a0 To get these reading, open a shell and enter in this command.<\/p>\n<pre>\r\n<code>\r\n\/opt\/vc\/bin\/vcgencmd measure_temp\r\ntemp=<span style=\"color:red\">46.0'C<\/span>\r\n<\/code>\r\n<\/pre>\n<p>The reading should appear below the command after you press enter.\u00a0 Here comes the problem.\u00a0 We need to get the numeric value over to Cacti.\u00a0 Do do that I used this script to get the correct format that Cacti can use.<\/p>\n<pre>\r\n<code>\r\n#!\/bin\/bash\r\n\r\nchiptempdir=\/var\/www\/cacti\/scripts\/chiptemp.txt\r\n\/opt\/vc\/bin\/vcgencmd measure_temp &gt; \"$chiptempdir\"\r\nchiptemp=$(&lt;\/var\/www\/cacti\/scripts\/chiptemp.txt)\r\nchiptemp=${chiptemp:5:2}\r\n\r\nchiptemphead='rpitemp:'\r\necho \"$chiptemphead$chiptemp\" &gt; \"$chiptempdir\"\r\n\r\nchiptempvalue=$(&lt;\/var\/www\/cacti\/scripts\/chiptemp.txt)\r\necho \"$chiptempvalue\"\r\n<\/code>\r\n<\/pre>\n<p>Cacti took to it like bear to honey.\u00a0 It plotted everything as expected, then my router died.\u00a0 That&#8217;s one of the dependencies for web scraping, internet access.\u00a0 It seems obvious, but I wanted to point that out.\u00a0 If you plan to use this function for critical applications, be sure you factor in that.<\/p>\n<p><strong>Relations &#8211; expanding control functions<\/strong><\/p>\n<p>Using web sources has its benefits and its risks.\u00a0 It can save time with sensor development and deployment.\u00a0 However, it increases the risk of disruption because web content is not controlled.\u00a0 Using the content for validation is one way to reduce the risk.\u00a0 The reliance of the data should be a factor in the design.<\/p>\n<p>There are disclaimers on many data source outlets.\u00a0 It is a risk if critical systems depend solely on these sources to make decisions.\u00a0 The key to successfully using third party data sources is to diversify and validate<\/p>\n<p><strong>Summary &#8211; data is a commodity<\/strong><\/p>\n<p>We covered some concepts of data mining.\u00a0 In this post we showed how to gather data from internal and external sources.\u00a0 Concepts of reliability and contingency were also covered.\u00a0 With these skills, advanced topics of system control and monitoring can be covered.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction &#8211; how&#8217;s it going This post will cover the logging of data in Cacti using non SNMP\/WMI sources.\u00a0 We&#8217;ll gather local RPi information and plot it in Cacti.\u00a0 Then we will go through the procedures to gather data from the internet and process it into Cacti.\u00a0 This is an expansion of the NOAA XML file processing done in an earlier post. Purpose &#8211; having a reference can give you an idea of were you&#8217;re going Cacti is a multi&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=1824\"> 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,6,3],"tags":[],"class_list":["post-1824","post","type-post","status-publish","format-standard","hentry","category-data-mining","category-raspberry-pi","category-rd"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1824","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=1824"}],"version-history":[{"count":28,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1824\/revisions"}],"predecessor-version":[{"id":1900,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1824\/revisions\/1900"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}