Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //date 4/14/2016
- //Smupper v3
- //author: Jeff Swenson js@mt.net j.swenson@simplixity.net
- ///user snmpget to retrieve ap information.
- //set date variable
- $date = date("Y/m/d");
- //connect to database via pdo
- $host = '127.0.0.1'; //host variable
- $db = 'adatabase';//database name variable
- $user = 'root';//database user variable
- $pass = "passstring**";//database user password variable
- $charset = 'utf8';//database charset type variable
- $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
- $opt = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- $pdo = new PDO($dsn, $user, $pass, $opt);
- //this is a counter
- $acount = '0';
- //load sm list file: this is a list of IPS
- $array = array(); // this is the array for the data from siteList.txt will be stored.
- $handle = fopen("single.txt", "r") or die("epic fail"); //this is a file for single testing
- //$handle = fopen("alist.txt", "r") or die("epic fail");// this is the main file
- //read through the list and put the IPS into an array
- while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
- $array[$row[0]] = $row[0];
- }
- fclose($handle);
- //display ammount of sites loaded in the array
- echo "there is ". count($array) . " sites loaded into the application" . "<br>";
- sort($array);
- //loop through each item in the array
- foreach($array as $key => $ip){
- $community = "passstring";
- //set the object ids
- $oid = ".1.3.6.1.2.1.1.5.0";
- $oid2 = ".1.3.6.1.4.1.161.19.3.1.7.1.0";
- $oidCC = ".1.3.6.1.4.1.161.19.3.1.10.1.1.6.1";
- //build the snmp2 get strings for each object id
- $test = snmp2_get($ip, $community, $oid);//ap name
- $test2 = snmp2_get($ip, $community, $oid2); //sm count
- $test3 = snmp2_get($ip, $community, $oidCC); //color code
- $acount++;//add one to count
- try{
- //echo $acount;
- //this is the variable for the ap name raw input
- $testa = substr($test, 5);
- //this is the variable for the sm count raw input
- $test2a = substr($test2, 5);
- //this is the variable for the color code raw input
- $test3a = substr($test3, 3);
- //this is for showing the raw output before anyhting is trimmed
- //print "ap name :" . $test . "sm count :" . $test2 . "<br>";
- //ap name :STRING: "HOGBK-SW-2.4-FSK-2 {A13098};VS 180deg"sm count :Gauge32: 23
- //trim the values in the array so they are human readable
- //ap trims as expected "HOGBK-SW-2.4-FSK-2
- $ap = explode(' {',substr($test,strpos($test,': ') + 2))[0];
- //count trim
- //$count = explode(': ', $test2)[1];//this does
- //trying to implement preg_match instead of explode + strpos
- preg_match('/(?P<node>^.[A-Za-z0-9\.-]+)/', $test, $matches);
- print_r($matches);// this should print a number
- //this is not needed at this time | to display color code of said ap
- //. " color code: " . substr($test3,strpos($test3,': ') + 2);
- //insert into the database with pdo
- //this does not seem to work at this point
- $stmt = $pdo->prepare("INSERT INTO nodeCounts(NodeID, Count, DateLogged) VALUES (:NodeID, :Count, :DateLogged)");
- $stmt->bindParam(':NodeID', $ap);
- $stmt->bindParam(':Count', $count);
- $stmt->bindParam(':DateLogged', $date);
- //echo the date
- echo "<br>" . $date . "<br>";
- //echo the snmp ap name
- echo $ap ."<br>";
- //echo the amount of registered sm to ap
- echo $count ."<br>";//this just echos "Array"
- echo "<br>";
- } catch (PDOException $e) {
- echo "Error: " . $e->getMessage();
- }//end pdo insert to database.
- }
- //close pdo connection
- $pdo = null;
- ?>
Add Comment
Please, Sign In to add comment