Advertisement
notrin

Untitled

Apr 15th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2. //date 4/14/2016
  3. //Smupper v3
  4. //author: Jeff Swenson js@mt.net j.swenson@simplixity.net
  5. ///user snmpget to retrieve ap information.
  6.  
  7. //set date variable
  8. $date = date("Y/m/d");
  9.  
  10. //connect to database via pdo
  11. $host = '127.0.0.1'; //host variable
  12. $db = 'adatabase';//database name variable
  13. $user = 'root';//database user variable
  14. $pass = "MontanaNet4**";//database user password variable
  15. $charset = 'utf8';//database charset type variable
  16.  
  17. $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
  18.  
  19. $opt = [
  20. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  21. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  22. PDO::ATTR_EMULATE_PREPARES => false,
  23. ];
  24.  
  25. $pdo = new PDO($dsn, $user, $pass, $opt);
  26.  
  27. //this is a counter
  28. $acount = '0';
  29.  
  30. //load sm list file: this is a list of IPS
  31. $array = array(); // this is the array for the data from siteList.txt will be stored.
  32. $handle = fopen("alist.txt", "r") or die("epic fail");
  33.  
  34. //read through the list and put the IPS into an array
  35. while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
  36. $array[$row[0]] = $row[0];
  37. }
  38. fclose($handle);
  39.  
  40. //display ammount of sites loaded in the array
  41. echo "there is ". count($array) . " sites loaded into the application" . "<br>";
  42.  
  43. sort($array);
  44. //loop through each item in the array
  45. foreach($array as $key => $ip){
  46. $community = "micadmpasscomm";
  47. //set the object ids
  48. $oid = ".1.3.6.1.2.1.1.5.0";
  49. $oid2 = ".1.3.6.1.4.1.161.19.3.1.7.1.0";
  50. $oidCC = ".1.3.6.1.4.1.161.19.3.1.10.1.1.6.1";
  51.  
  52. //build the snmp2 get strings for each object id
  53. $test = snmp2_get($ip, $community, $oid);//ap name
  54. $test2 = snmp2_get($ip, $community, $oid2); //sm count
  55. $test3 = snmp2_get($ip, $community, $oidCC); //color code
  56.  
  57. $acount++;//add one to count
  58. try{
  59. //echo $acount;
  60.  
  61. //this is the variable for the ap name raw input
  62. $testa = substr($test, 5);
  63. //this is the variable for the sm count raw input
  64. $test2a = substr($test2, 5);
  65. //this is the variable for the color code raw input
  66. $test3a = substr($test3, 3);
  67.  
  68. //this is for showing the raw output before anyhting is trimmed
  69. //print "ap name :" . $test . "sm count :" . $test2 . "<br>";
  70. //ap name :STRING: "HOGBK-SW-2.4-FSK-2 {A13098};VS 180deg"sm count :Gauge32: 23
  71.  
  72.  
  73. //trim the values in the array so they are human readable
  74. //ap trims as expected "HOGBK-SW-2.4-FSK-2
  75. $ap = explode(' {',substr($test,strpos($test,': ') + 2))[0];
  76.  
  77. //count does not trim as expected Gauge32: 23
  78. //this does not work *****************
  79. $count = explode(' {',substr($test2,strpos($test2,':') - 7));
  80. //this does not work *****************
  81.  
  82. //this is not needed at this time | to display color code of said ap
  83. //. " color code: " . substr($test3,strpos($test3,': ') + 2);
  84.  
  85. //insert into the database with pdo
  86. //this does not seem to work either
  87. $stmt = $pdo->prepare("INSERT INTO nodeCounts(NodeID, Count, DateLogged) VALUES (:NodeID, :Count, :DateLogged)");
  88.  
  89. $stmt->bindParam(':NodeID', $ap);
  90. $stmt->bindParam(':Count', $count);
  91. $stmt->bindParam(':DateLogged', $date);
  92. //echo the date
  93. echo $date ."<br>";
  94.  
  95. //echo the snmp ap name
  96. echo $ap ."<br>";//this echos "acceptable" but still has issues because of needing a different needle for the haystack
  97.  
  98. //echo the amount of registered sm to ap
  99.  
  100. //this does not work *****************
  101. echo $count ."<br>";//this just echos "Array"
  102. //if i echo $test2 i get Gauge32: 38
  103. //when i should echo $count i should get 38
  104. //this does not work *****************
  105.  
  106. } catch (PDOException $e) {
  107. echo "Error: " . $e->getMessage();
  108. }//end pdo insert to database.
  109. }
  110. $pdo = null;
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement