{"id":343,"date":"2015-05-30T18:56:59","date_gmt":"2015-05-30T17:56:59","guid":{"rendered":"http:\/\/tricker.cz\/?p=343"},"modified":"2017-04-21T08:06:48","modified_gmt":"2017-04-21T07:06:48","slug":"generating-smokeping-probes-and-targets-with-php-and-mysql","status":"publish","type":"post","link":"https:\/\/tricker.cz\/?p=343","title":{"rendered":"Generating Smokeping probes and targets with PHP and MySQL"},"content":{"rendered":"<p>Well, the concept is so simple, that of course there is no limit in language and database. You should modify paths and other credentials in code. This is just basic example how you could achieve such thing.<\/p>\n<p>PHP file that will generete part of our smokeping config with Probes and Targets on server with database:<\/p>\n<pre class=\"toolbar:1 lang:php decode:true\" title=\"ProbeTargets.php\">&lt;?php\r\n\r\n  \/\/ Connect to database\r\n  if(!($GLOBALS[\"DBC\"] = mysqli_connect(\"server\",  \"user_db\",  \"pass_db\"))) die (\"Cannot connect to database.\");\r\n  if(!((bool)mysqli_query($GLOBALS[\"DBC\"], \"USE $db\"))) die (\"Cannot choose database {$db}.\");\r\n\r\n  \/\/ Get devices to monitor\r\n  $q = mysqli_query($GLOBALS[\"DBC\"], \"SELECT ID, IP, name \r\n                                      FROM devices;\");\r\n\r\n  $rows = mysqli_num_rows($q);\r\n\r\n  \/\/ How many probes do we need? One for 100 devices\r\n  $probes = floor($rows \/ 100) + 1;\r\n\r\n  \/\/ Output\r\n  $o = \"\";\r\n\r\n  \/\/ Basic probes configuration - We will keep it with Targets in one file\r\n  $o .= \"*** Probes ***\\n\\n\";\r\n\r\n  \/\/ Be sure that you have fping installed and in right location\r\n  $o .= \"+ FPing\\n\";\r\n  $o .= \"binary = \/usr\/bin\/fping\\n\\n\";\r\n\r\n  for($a = 0; $a &lt; $probes; $a++)\r\n  {\r\n\r\n    $o .= \"++FPing{$a}\\n\\n\";\r\n\r\n    $o .= \"blazemode = true\\n\";\r\n    $o .= \"hostinterval = 1.5\\n\";\r\n    $o .= \"mininterval = 0.001\\n\";\r\n    $o .= \"offset = 50% \\n\";\r\n    $o .= \"packetsize = 56\\n\";\r\n    $o .= \"pings = 20\\n\";\r\n    $o .= \"step = 60\\n\";\r\n    $o .= \"timeout = 1.5\\n\";\r\n    $o .= \"tos = 0x20\\n\";\r\n    $o .= \"#usestdout = true\\n\\n\";\r\n\r\n  }\r\n\r\n  \/\/ There goes our targets\r\n  $o .= \"*** Targets ***\\n\\n\";\r\n\r\n  $o .= \"probe = FPing0\\n\\n\";\r\n\r\n  $o .= \"menu = Top\\n\";\r\n  $o .= \"title = SmokePing\\n\";\r\n  $o .= \"remark = \\n\\n\";\r\n\r\n  $o .= \"+ SMOKE\\n\";\r\n  $o .= \"menu = Hosts\\n\\n\";\r\n\r\n  \/\/ Let's count to determine probe number\r\n  $a = 0;\r\n\r\n  while($r = mysqli_fetch_assoc($q))\r\n  {\r\n\r\n    $probe = floor($a \/ 100);\r\n\r\n    $o .= \"++ ID{$r['ID']}\\n\\n\";\r\n\r\n    $o .= \"probe = FPing{$probe}\\n\";\r\n    $o .= \"menu = {$r['name']}\\n\";\r\n    $o .= \"title = {$r['name']} (IP: {$r['IP']}, ID: {$r['ID']})\\n\";\r\n    $o .= \"host = {$r['IP']}\\n\\n\";\r\n\r\n    $a++;\r\n\r\n  }\r\n\r\n  echo $o;<\/pre>\n<p>Script to download configuration and restart smokeping if something changes:<\/p>\n<pre class=\"toolbar:1 lang:sh decode:true \" title=\"smokepingUpdate.sh\">#!\/bin\/bash\r\n\r\n# Download file with our configuration\r\nwget -q --no-check-certificate -O \/etc\/tricker\/smoketricker.conf https:\/\/monitoring.tricker.cz\/scripts\/export\/smokeping\/ProbesTargets.php\r\n\r\n# How large our file is? Do we have something?\r\nvelikost=$(wc -c \"\/etc\/tricker\/smoketricker.conf\" | cut -f 1 -d ' ')\r\n\r\n# Have sometnig? Continue\r\nif [ $velikost -ge 500 ]; then\r\n\r\n  # Checking if our files are the same, use correct paths!\r\n  md5one=$(md5sum \/etc\/tricker\/smoketricker.conf | cut -f 1 -d ' ')\r\n  md5two=$(md5sum \/opt\/smokeping-2.6.11\/etc\/smoketricker.conf | cut -f 1 -d ' ')\r\n\r\n  # Do we have something different? Reload smokeping.\r\n  if [ $md5one != $md5two ]\r\n  then\r\n\r\n    # Again - Check paths! I used compiled smokeping with version in path\r\n    cp \/etc\/tricker\/smoketricker.conf \/opt\/smokeping-2.6.11\/etc\/smoketricker.conf\r\n    kill -TERM $(cat \/opt\/smokeping-2.6.11\/var\/smokeping.pid)\r\n    \/opt\/smokeping-2.6.11\/bin\/smokeping --config=\/opt\/smokeping-2.6.11\/etc\/config --logfile=\/var\/log\/smoke.log\r\n\r\n  fi\r\n\r\nfi<\/pre>\n<p>CRON to periodically call <strong>smokepingUpdate.sh<\/strong>:<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">*\/10 * * * * \/home\/tricker\/smokepingUpdate.sh<\/pre>\n<p>In <strong>smokeping\/etc\/config<\/strong> you should remove section &#8222;Targets&#8220; and &#8222;Probes&#8220;, because they are generated by script and add this line at the end of file to start loading our config:<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">@include smoketricker.conf<\/pre>\n<p>I use this to start Smokeping:<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">\/opt\/smokeping-2.6.11\/bin\/smokeping --config=\/opt\/smokeping-2.6.11\/etc\/config --logfile=\/var\/log\/smoke.log<\/pre>\n<p>That&#8217;s all! Just one more tip &#8211; For those who need (just as I) to show graphs in remote system and you can&#8217;t (or just simply want) generate all graphs static (Smokeping can do that for you, possible SSD killer)&#8230; Generate graphs by calling something like this before showing them:<\/p>\n<pre class=\"toolbar:1 lang:php decode:true \">&lt;?php\r\n\r\n  $ch = curl_init();\r\n  curl_setopt($ch, CURLOPT_URL, $n['smokeping_url'].\"smokeping.cgi?target=SMOKE.ID\".intval($_GET['id']));\r\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);\r\n  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);\r\n  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\r\n  $r = curl_exec($ch);\r\n  $e = curl_error($ch);\r\n  curl_close($ch);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Well, the concept is so simple, that of course there is no limit in language and database. You should modify paths and other credentials in code. This is just basic example how you could achieve such thing. PHP file that&#8230; <a class=\"more-link\" href=\"https:\/\/tricker.cz\/?p=343\">Pokra\u010dovat ve \u010dten\u00ed &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/343"}],"collection":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":7,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}