{"id":1288,"date":"2014-12-11T00:00:12","date_gmt":"2014-12-11T08:00:12","guid":{"rendered":"http:\/\/192.168.3.4\/?p=1288"},"modified":"2018-01-09T06:49:50","modified_gmt":"2018-01-09T14:49:50","slug":"dual-camera-helmet","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=1288","title":{"rendered":"Cacti, RPi, and GPIO device readings"},"content":{"rendered":"<p><strong>Introduction &#8211; CactiPi isn&#8217;t as hard to eat as you might think.<br \/>\n<\/strong><\/p>\n<p>In my <a href=\"http:\/\/192.168.3.4\/?p=1444\" target=\"_blank\">earlier post<\/a>, the\u00a0RPi command and control features have been an excellent way to show capabilities. Webmin showed how complex admin tasks can be done graphiclly, without the need for a desktop environment. Smokeping was introduced, but not covered in detail. This tool is usefull in identifying host availability on the network, but it doesn&#8217;t go much further.<\/p>\n<p>This is were Cacti comes into play. This is primarily a graphing utility based off of <a href=\"https:\/\/tobi.oetiker.ch\/hp\/\" target=\"_blank\">Tobi Oetiker&#8217;s MRTG and RRDTool<\/a> efforts. I have been using these tools on systems\u00a0longer than the existance of the RPi and Arduino platforms combined. Cacti is interesting due to its support of third party plugins. This is what makes the tool take on a different form and extends it to different disciplines.<\/p>\n<p>Here is a description, that I found at\u00a0<a href=\"http:\/\/openmaniak.com\/cacti.php\" target=\"_blank\">http:\/\/openmaniak.com\/cacti.php<\/a>,\u00a0of what Cacti is.<\/p>\n<p><em>&#8220;Cacti is a web based PHP\/MySql graphing solution using the RRDtool engine. \u00a0<\/em><em>Classically, it can graph network bandwidthes with SNMP. But in fact, a lot of different graphs can be done with snmp, shell or perl scripts&#8230;<\/em><em>Cacti&#8217;s strength lies in the fact that it can be installed and used incredibly easily&#8230; On the very active Cacti forum, you can share &#8220;Cacti templates&#8221; with other users which can can save you a lot of time. You can very easily add plugins to the Cacti too enabling the possiblility to integrate other free tools like ntop or php weathermap..<\/em><em>.<\/em><em>RRDtool is developed using the &#8220;C&#8221; programming language and it stores the collected data on &#8220;.rrd&#8221; files.<\/em><em>The number of records in a &#8220;.rrd&#8221; file never increases, meaning that old records are frequently removed. This implies that one obtains precise figures for recently logged data, whereas figures based on very old data are mean value approximations. By default, you can have daily, weekly, monthy and yearly graphs.&#8221;<\/em><\/p>\n<p>Since the hard work of Cacti and getting it installed on RPi have been ironed out, lets dive in.<\/p>\n<p><strong>Purpose &#8211; Don&#8217;t reinvent the wheel, just learn how to use it.<br \/>\n<\/strong><\/p>\n<p>In this section, I will go through what Cacti is and how it can be used on a RPi. I&#8217;ll also cover the installation and setup of Cacti on the RPi. Cacti comes bundled with features to allow it to monitor and graph metrics like processes, cpu usage, network traffic, and latency. I&#8217;ll also cover how to setup Cacti to poll and graph SNMP devices using non standard OID values. Leastly, I&#8217;ll cover how to use Cacti to poll and graph devices attached through the RPi GPIO header. The ability to quantify values using the RPi will be worthwhile. I&#8217;ve used VMs to perform these tasks as a solution for network diagnostics. The VMs are a point of vulnerablity and are also dependent on the host they run on. The RPi is well suited to run along side the VMs to add a layer of reliablity without high costs.<\/p>\n<p><strong>Detail &#8211; Having a way to look back at where you&#8217;ve been.<br \/>\n<\/strong><\/p>\n<p>The steps to install Cacti on the RPi are <a href=\"http:\/\/n00blab.com\/cacti-pi\/\" target=\"_blank\">available here<\/a>. \u00a0You can see that the steps aren&#8217;t too involving. \u00a0I had success going through the prodecures. \u00a0However, it is worth mentioning that it is good practice to\u00a0included the sudo command at the beginning of each string.<br \/>\n<iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/yX8oR-Fkj9M\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>These steps also assume that fundamental things, like LAMP have already been installed. \u00a0If you&#8217;ve been following along with my posts, then you should be set. \u00a0Otherwise, go back to my post about <a href=\"http:\/\/192.168.3.4\/?p=1420\" target=\"_blank\">RPi basics<\/a> and start there.<\/p>\n<p>This was super easy to do and Cacti is now running with some preloaded graphs about the RPi processor, memory, and network usage. \u00a0But that isn&#8217;t all we&#8217;ll be doing with Cacti. \u00a0Since it can poll SNMP devices, I&#8217;ve added a couple of my routers and storage hosts. \u00a0The built in templates can handle most SNMP needs, so adding these devices was just as easy as the install.<\/p>\n<p>But there was\u00a0a <a href=\"http:\/\/www.itwatchdogs.com\/climate-monitor-weathergoose-ii-p1.html\" target=\"_blank\">temperature device<\/a>\u00a0I wanted to graph and Cacti didn&#8217;t have a template to handle the SNMP OID. \u00a0For this I would need to identify the OIDs and create custom data, graph, and host templates in Cacti. \u00a0It seems like a lot, but it really wasn&#8217;t that bad. \u00a0Compared to the suggestions I found online that had me creating an XML file from scratch and importing it, it was a snap.<\/p>\n<p>First thing I did was <a href=\"http:\/\/cuddletech.com\/articles\/snmp\/node13.html\" target=\"_blank\">snmpwalk<\/a> the device to list all of the OIDs. \u00a0Then I could select the one that pertained to the data I wanted to graph.<\/p>\n<p><em>snmpwalk -v1 -c public [ip_of_device]\u00a0.1.3.6.1.4.1<\/em><\/p>\n<p><em>.1.3.6.1.4.1.17373.2.4.1.3.1 = STRING: &#8220;Attic&#8221;<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.3.2 = STRING: &#8220;Front Gable&#8221;<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.3.3 = STRING: &#8220;Rear Door&#8221;<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.3.4 = STRING: &#8220;Hot Water Return&#8221;<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.3.5 = STRING: &#8220;Living Room&#8221;<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.5.1 = INTEGER: 10 ^<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.5.2 = INTEGER: 8 ^<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.5.3 = INTEGER: 14 ^<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.5.4 = INTEGER: 40<\/em><br \/>\n<em>.1.3.6.1.4.1.17373.2.4.1.5.5 = INTEGER: 21<\/em><\/p>\n<p>The steps I then followed can be found in the book &#8220;<a href=\"https:\/\/www.packtpub.com\/networking-and-servers\/cacti-08-beginners-guide\" target=\"_blank\">Cacti 0.8 Beginner&#8217;s Guide<\/a>&#8221; from\u00a0Thomas Urban. \u00a0I\u00a0defined the data template using the steps outlined on pages 55 &#8211; 56. \u00a0Next I\u00a0defined the graph template using steps on pages\u00a059 &#8211; 60. \u00a0In this section I also\u00a0added graph template\u00a0items,\u00a0these are the lines and areas in the rrd graph, and\u00a0graph item inputs,\u00a0these are the rrd sources to graph. \u00a0Once that was complete, I then\u00a0defined the host template outlined on pages 66 &#8211; 67.<\/p>\n<p>Now all I had to do was add the device in Cacti and\u00a0add the associated graph templates for the device, then create new graphs for the device.<\/p>\n<p>I highly recommend getting the book from Thomas Urban. \u00a0It covers material that is outside the scope of this post, and will make it much easier to follow along with the steps above. \u00a0Seriously, do yourself a service, get the book.<\/p>\n<p>So far we&#8217;ve covered the extent of what Cacti can do on any\u00a0host system. \u00a0Here&#8217;s what raises the RPi above the rest, graphing data through the GPIO header. \u00a0I was a bit intimidated at first but managed to find <a href=\"http:\/\/jartweb.net\/blog\/wp-content\/uploads\/2013\/12\/Raspberry-Pi-Logger-with-LCD.pdf\" target=\"_blank\">a decent write up<\/a> on how to gather and graph non SNMP data with Cacti. \u00a0This was a <a href=\"http:\/\/blog.jartweb.net\/?p=999\" target=\"_blank\">gold mine<\/a> find. \u00a0Now all I had to do was connect something to the GPIO header and read it.<\/p>\n<p>I settled on an analog device that reads light intensity. \u00a0For that I would use a photo resistor and a 2 channel MCP3002 ADC that is similar to the <a href=\"https:\/\/learn.adafruit.com\/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi\/connecting-the-cobbler-to-a-mcp3008\" target=\"_blank\">example given<\/a> by Adafruit. \u00a0I had to <a href=\"https:\/\/learn.adafruit.com\/adafruits-raspberry-pi-lesson-4-gpio-setup\/overview\" target=\"_blank\">load some libraries<\/a> to read the ADC, but once they were loaded my python script (<a href=\"http:\/\/jeremyblythe.blogspot.ca\/2012\/09\/raspberry-pi-hardware-spi-analog-inputs.html\" target=\"_blank\">based off of Jeremy&#8217;s Blog<\/a>) was ready. \u00a0Since all the working details of Python are\u00a0beyond the scope of this post, you can find\u00a0more on learning Python at\u00a0<a href=\"http:\/\/www.codecademy.com\/en\/tracks\/python\/\" target=\"_blank\">Codecademy<\/a>. \u00a0Anyway, here&#8217;s the code I used.<\/p>\n<p><em>\u00a0#!\/usr\/bin\/python<\/em><\/p>\n<p><em>import spidev<\/em><br \/>\n<em>import time<\/em><\/p>\n<p><em>spi = spidev.SpiDev()<\/em><br \/>\n<em>spi.open(0, 0)<\/em><\/p>\n<p><em>def readadc(adcnum):<\/em><br \/>\n<em># read SPI data from MCP3008 chip, 8 possible adc&#8217;s (0 thru 7)<\/em><br \/>\n<em># read SPI data from MCP3002 chip, 2 possible adc&#8217;s (0 thru 1)<\/em><br \/>\n<em> #if adcnum &gt; 7 or adcnum &lt; 0:<\/em><br \/>\n<em> if adcnum &gt; 1 or adcnum &lt; 0:<\/em><br \/>\n<em> return -1<\/em><br \/>\n<em> r = spi.xfer2([1, 8 + adcnum &lt;&lt; 4, 0])<\/em><br \/>\n<em> adcout = ((r[1] &amp; 3) &lt;&lt; 8) + r[2]<\/em><br \/>\n<em> return adcout<\/em><\/p>\n<p><em>value = readadc(0)<\/em><br \/>\n<em>print &#8220;lux:%d&#8221; % value<\/em><\/p>\n<p>Now with the python script doing what it should, I copied the script to my Cacti scripts folder and made the script executable with this commnad:<\/p>\n<p><em>sudo chmod 777 \/var\/www\/cacti\/scripts\/rpi_light_sens.py<\/em><\/p>\n<p>Everything was in place to step through the <a href=\"http:\/\/jartweb.net\/blog\/wp-content\/uploads\/2013\/12\/Raspberry-Pi-Logger-with-LCD.pdf\" target=\"_blank\">write up<\/a> pages 24-36. \u00a0Walla! \u00a0I&#8217;m graphing data from the GPIO into Cacti.<\/p>\n<p><a href=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2014\/12\/graph_image.php_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1571\" src=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2014\/12\/graph_image.php_.png\" alt=\"graph_image.php\" width=\"595\" height=\"196\" srcset=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_.png 595w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_-300x98.png 300w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_-100x32.png 100w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_-150x49.png 150w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_-200x65.png 200w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2014\/12\/graph_image.php_-450x148.png 450w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/a><\/p>\n<p><strong>Relations &#8211; Cacti is here to stay, so what else can it do?<br \/>\n<\/strong><\/p>\n<p>What else can Cacti do?\u00a0 It depends on what plugin you want to load.\u00a0 The list is fairly extensive, have a<a href=\"http:\/\/docs.cacti.net\/plugins\" target=\"_blank\"> look here<\/a>.\u00a0 Some of the more popular ones are syslog, thold, and weathermap.\u00a0 Syslog is great for gathering system messages and listing them.\u00a0 I&#8217;ve been able to find a server disk controller fault this way.\u00a0 Without it, I wouldn&#8217;t know what caused the server to die.\u00a0 Thold is useful for sending alerts based on thresholds in the data.\u00a0 Is the disk too full, you&#8217;ll get an alert.\u00a0 Weathermap is great for identifying the overall health of what you&#8217;re tracking.\u00a0 It&#8217;s visual, so it&#8217;s quick and easy.\u00a0 It&#8217;s worth mentioning here that any plugin will require the Cacti Plugin Architecture.\u00a0 This is the requirement, because\u00a0it is a foundation for the plugins to work. \u00a0But be warned, it is a tremendous pain in the ass to install, if your cacti version isn&#8217;t the same as the PIA patch. \u00a0I&#8217;ve managed to undo a Cacti install with a botched patch. \u00a0It isn&#8217;t for the lighthearted or casual admin. \u00a0But if you&#8217;re bold and want to explore the world of Cacti plugins, <a href=\"http:\/\/openmaniak.com\/cacti_plugins.php\" target=\"_blank\">start here<\/a>. \u00a0Regardless, there is some good news after all, the newer versions of Cacti come with the PIA preloaded.<\/p>\n<p>With all these options available, the\u00a0RPi will only do what its hardware allows.\u00a0 At some point, you could ask too much of it and notice a degrade in performace.\u00a0 The same is true of humans.\u00a0 Just because you can&#8217;t see the bottom of the well, doesn&#8217;t mean you have an endless supply of water.<\/p>\n<p>So the question that&#8217;s unanswered is, does Cacti really scale well?\u00a0 I don&#8217;t know.\u00a0 It may see off subject but I really liked this presentation.\u00a0 If anything it&#8217;s a lesson learned for experiences of others using Nagios. \u00a0These lessons can be used to critically look at Cacti in a realistic way, not just gazing at pretty graphs.<br \/>\n<iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/Q9BagdHGopg\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><strong>Summary &#8211; This one was packed to the hilt.<br \/>\n<\/strong><\/p>\n<p>Seriously, this post has been the most jam packed with material. \u00a0We&#8217;ve covered the steps to install Cacti on the RPi. \u00a0Once installed we could see the default metrics of our RPi and add devices that offer SNMP polling. \u00a0From there we added custom OID polling to non template SNMP devices. \u00a0This expanded the graphing range of Cacti and allows it to cover a greater range of resources. \u00a0Next, we attached sensors to the RPi GPIO header and created a Python script to represent the reading in a format that was Cacti compatible. \u00a0Next we set the parameters on Cacti to execute the script and read the values into a graph. \u00a0Finally, we covered some extensible options and limitations of the RPi and Cacti. \u00a0From here, you should be able to deploy a RPi as a metric polling device and have it provide the results in a web browser.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction &#8211; CactiPi isn&#8217;t as hard to eat as you might think. In my earlier post, the\u00a0RPi command and control features have been an excellent way to show capabilities. Webmin showed how complex admin tasks can be done graphiclly, without the need for a desktop environment. Smokeping was introduced, but not covered in detail. This tool is usefull in identifying host availability on the network, but it doesn&#8217;t go much further. This is were Cacti comes into play. This is&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=1288\"> 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":[2,10,6,3],"tags":[],"class_list":["post-1288","post","type-post","status-publish","format-standard","hentry","category-bike-rides","category-data-mining","category-raspberry-pi","category-rd"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1288","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=1288"}],"version-history":[{"count":61,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}