notrin

Untitled

Apr 15th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 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 = "passstring**";//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.  
  33. $handle = fopen("single.txt", "r") or die("epic fail"); //this is a file for single testing
  34.  
  35.  
  36. //$handle = fopen("alist.txt", "r") or die("epic fail");// this is the main file
  37.  
  38. //read through the list and put the IPS into an array
  39. while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
  40. $array[$row[0]] = $row[0];
  41. }
  42. fclose($handle);
  43.  
  44. //display ammount of sites loaded in the array
  45. echo "there is ". count($array) . " sites loaded into the application" . "<br>";
  46.  
  47. sort($array);
  48. //loop through each item in the array
  49. foreach($array as $key => $ip){
  50. $community = "passstring";
  51. //set the object ids
  52. $oid = ".1.3.6.1.2.1.1.5.0";
  53. $oid2 = ".1.3.6.1.4.1.161.19.3.1.7.1.0";
  54. $oidCC = ".1.3.6.1.4.1.161.19.3.1.10.1.1.6.1";
  55.  
  56. //build the snmp2 get strings for each object id
  57. $test = snmp2_get($ip, $community, $oid);//ap name
  58. $test2 = snmp2_get($ip, $community, $oid2); //sm count
  59. $test3 = snmp2_get($ip, $community, $oidCC); //color code
  60.  
  61. $acount++;//add one to count
  62. try{
  63. //echo $acount;
  64.  
  65. //this is the variable for the ap name raw input
  66. $testa = substr($test, 5);
  67. //this is the variable for the sm count raw input
  68. $test2a = substr($test2, 5);
  69. //this is the variable for the color code raw input
  70. $test3a = substr($test3, 3);
  71.  
  72. //this is for showing the raw output before anyhting is trimmed
  73. //print "ap name :" . $test . "sm count :" . $test2 . "<br>";
  74. //ap name :STRING: "HOGBK-SW-2.4-FSK-2 {A13098};VS 180deg"sm count :Gauge32: 23
  75.  
  76.  
  77. //trim the values in the array so they are human readable
  78. //ap trims as expected "HOGBK-SW-2.4-FSK-2
  79.  
  80. $ap = explode(' {',substr($test,strpos($test,': ') + 2))[0];
  81.  
  82. //count trim
  83. //$count = explode(': ', $test2)[1];//this does
  84.  
  85. //trying to implement preg_match instead of explode + strpos
  86. preg_match('/(?P<node>^.[A-Za-z0-9\.-]+)/', $test, $matches);
  87. print_r($matches);// this should print a number
  88.  
  89. //this is not needed at this time | to display color code of said ap
  90. //. " color code: " . substr($test3,strpos($test3,': ') + 2);
  91.  
  92. //insert into the database with pdo
  93. //this does not seem to work at this point
  94. $stmt = $pdo->prepare("INSERT INTO nodeCounts(NodeID, Count, DateLogged) VALUES (:NodeID, :Count, :DateLogged)");
  95. $stmt->bindParam(':NodeID', $ap);
  96. $stmt->bindParam(':Count', $count);
  97. $stmt->bindParam(':DateLogged', $date);
  98. //echo the date
  99. echo "<br>" . $date . "<br>";
  100.  
  101. //echo the snmp ap name
  102. echo $ap ."<br>";
  103.  
  104. //echo the amount of registered sm to ap
  105. echo $count ."<br>";//this just echos "Array"
  106. echo "<br>";
  107.  
  108. } catch (PDOException $e) {
  109. echo "Error: " . $e->getMessage();
  110. }//end pdo insert to database.
  111. }
  112.  
  113. //close pdo connection
  114. $pdo = null;
  115. ?>
Add Comment
Please, Sign In to add comment