[SOLVED] Format SNMP String

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
acasto
Posts: 38
Joined: Tue Mar 03, 2009 9:00 am

[SOLVED] Format SNMP String

Post by acasto »

I just have a quick question as to whether or not this is even worth pursuing. In quite a few instance I have to utilize a script to do basic formatting of a returned SNMP string. For example, chopping " dbm" off the end of a Motorola Canopy power level. So, I guess my first question is, is there some non-script way to do this from the data template that I've overlooked? If not, would it be worth it performance wise (when polling hundreds of hosts) to add a str_replace() instance to format_snmp_string function in lib/snmp.php to do this?

thanks,

Adam
User avatar
TheWitness
Developer
Posts: 17007
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Post by TheWitness »

Yes, we peel off all alpha characters and return numeric when performing get's (or at least we used to). I have not looked at this code for about a month, and before that, well a long time. However, we don't do this all the time, so I'm now wondering where that code is, but haven't got the energy to open the file. Sounds silly doesn't it...

The method we use is to count backwards and remove bogus characters till be get to the numeric characters, and then assume all is ok. (I think...)

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
acasto
Posts: 38
Joined: Tue Mar 03, 2009 9:00 am

Post by acasto »

I pasted the function below in case anyone is curious. It looks like it:
  • 1.) Trims certain values listed in REGEXP_SNMP_TRIM
    2.) Strips off the OID if included
    3.) Returns if value is empty
    4.) Returns if value is numeric
    5.) Removes quotes and delimiters
    6.) Checks for "Wrong Type" returns
    7.) Remove invalid characters
    8.) Checks for and converts hex values
A couple of questions though.

Where it deals with invalid MIB files, where does $position come from? It only seems shows up there in that block. Does this mean it simply returns them backwards?

As far as formatting the value backwards down to a numerical one like you mentioned, am I overlooking the part that does that besides in the block mentioned above? If not that sounds like it would be more versatile than my initial idea of simply adding additional str_replace() instances.

Also, just out of curiosity, is there instances where someone would want non-numeric values returned? I've never heard of it myself but wondered since they can get through the format function here.

Thanks,
Adam

Code: Select all

define("REGEXP_SNMP_TRIM", "(hex|counter(32|64)|gauge|gauge(32|64)|float|ipaddress|string|integer):");

Code: Select all

function format_snmp_string($string, $snmp_oid_included) {
	$string = eregi_replace(REGEXP_SNMP_TRIM, "", trim($string));

	if ($snmp_oid_included) {
		/* strip off all leading junk (the oid and stuff) */
		$string_array = explode("=", $string);
		if (sizeof($string_array) == 1) {
			/* trim excess first */
			$string = trim($string);
		}else if ((substr($string, 0, 1) == ".") || (strpos($string, "::") >= 0)) {
			/* drop the OID from the array */
			array_shift($string_array);
			$string = trim(implode("=", $string_array));
		}else {
			$string = trim(implode("=", $string_array));
		}
	}

	/* return the easiest value */
	if ($string == "") {
		return $string;
	}

	/* now check for the second most obvious */
	if (is_numeric($string)) {
		return trim($string);
	}

	/* remove ALL quotes, and other special delimiters */
	$string = str_replace("\"", "", $string);
	$string = str_replace("'", "", $string);
	$string = str_replace(">", "", $string);
	$string = str_replace("<", "", $string);
	$string = str_replace("\\", "", $string);
	$string = str_replace("\n", " ", $string);
	$string = str_replace("\r", " ", $string);

	/* account for invalid MIB files */
	if (substr_count($string, "Wrong Type")) {
		$string = strrev($string);
		if ($position = strpos($string, ":")) {
			$string = trim(strrev(substr($string, 0, $position)));
		}else{
			$string = trim(strrev($string));
		}
	}

	/* Remove invalid chars */
	$k = strlen($string);
	for ($i=0; $i < $k; $i++) {
		if ((ord($string[$i]) <= 31) || (ord($string[$i]) >= 127)) {
			$string[$i] = " ";
		}
	}
	$string = trim($string);

	if ((substr_count($string, "Hex-STRING:")) ||
		(substr_count($string, "Hex:"))) {
		/* strip of the 'Hex-STRING:' */
		$string = eregi_replace("Hex-STRING: ?", "", $string);
		$string = eregi_replace("Hex: ?", "", $string);

		$string_array = split(" ", $string);

		/* loop through each string character and make ascii */
		$string = "";
		$hexval = "";
		$ishex  = false;
		for ($i=0;($i<sizeof($string_array));$i++) {
			if (strlen($string_array[$i])) {
				$string .= chr(hexdec($string_array[$i]));

				$hexval .= str_pad($string_array[$i], 2, "0", STR_PAD_LEFT);

				if (($i+1) < count($string_array)) {
					$hexval .= ":";
				}

				if ((hexdec($string_array[$i]) <= 31) || (hexdec($string_array[$i]) >= 127)) {
					if ((($i+1) == sizeof($string_array)) && ($string_array[$i] == 0)) {
						/* do nothing */
					}else{
						$ishex = true;
					}
				}
			}
		}

		if ($ishex) $string = $hexval;
	}elseif (preg_match("/(hex:\?)?([a-fA-F0-9]{1,2}(:|\s)){5}/", $string)) {
		$octet = "";

		/* strip of the 'hex:' */
		$string = eregi_replace("hex: ?", "", $string);

		/* split the hex on the delimiter */
		$octets = preg_split("/\s|:/", $string);

		/* loop through each octet and format it accordingly */
		for ($i=0;($i<count($octets));$i++) {
			$octet .= str_pad($octets[$i], 2, "0", STR_PAD_LEFT);

			if (($i+1) < count($octets)) {
				$octet .= ":";
			}
		}

		/* copy the final result and make it upper case */
		$string = strtoupper($octet);
	}elseif (preg_match("/Timeticks:\s\((\d+)\)\s/", $string, $matches)) {
		$string = $matches[1];
	}

	return $string;
}
    User avatar
    TheWitness
    Developer
    Posts: 17007
    Joined: Tue May 14, 2002 5:08 pm
    Location: MI, USA
    Contact:

    Post by TheWitness »

    This code block strips non-numeric characters:

    Code: Select all

     $k = strlen($string);
       for ($i=0; $i < $k; $i++) {
          if ((ord($string[$i]) <= 31) || (ord($string[$i]) >= 127)) {
             $string[$i] = " ";
          }
       }
       $string = trim($string); 
    You would not use the get method to return Alpha characters.

    TheWitness
    True understanding begins only when we realize how little we truly understand...

    Life is an adventure, let yours begin with Cacti!

    Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
    _________________
    Official Cacti Documentation
    GitHub Repository with Supported Plugins
    Percona Device Packages (no support)
    Interesting Device Packages


    For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
    acasto
    Posts: 38
    Joined: Tue Mar 03, 2009 9:00 am

    Post by acasto »

    I thought that code block just stripped out ASCII control (<=31) and extended (>=127) characters but still leaves everything in between. Am I reading it wrong?
    User avatar
    TheWitness
    Developer
    Posts: 17007
    Joined: Tue May 14, 2002 5:08 pm
    Location: MI, USA
    Contact:

    Post by TheWitness »

    Ouch, I should look instead of think. Spine does that though. Now I have to check cmd.php. One of these days they will both be the same.

    TheWitness
    True understanding begins only when we realize how little we truly understand...

    Life is an adventure, let yours begin with Cacti!

    Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
    _________________
    Official Cacti Documentation
    GitHub Repository with Supported Plugins
    Percona Device Packages (no support)
    Interesting Device Packages


    For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
    acasto
    Posts: 38
    Joined: Tue Mar 03, 2009 9:00 am

    Post by acasto »

    Haha.... same here. I went hunting for that because it seemed like an snmp string wasn't being formatted right, but I totally forgot it would be Spine that does the work (since that's what I'm using). Turns out it was working and I just forgot to set my minimum to a negative since radio power levels are given in -dBm. Oh well, at least I'm getting better acquainted with Cacti's innards.

    Thanks again,

    Adam
    User avatar
    TheWitness
    Developer
    Posts: 17007
    Joined: Tue May 14, 2002 5:08 pm
    Location: MI, USA
    Contact:

    Post by TheWitness »

    Good, I'll mark solved then.

    TheWitness
    True understanding begins only when we realize how little we truly understand...

    Life is an adventure, let yours begin with Cacti!

    Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
    _________________
    Official Cacti Documentation
    GitHub Repository with Supported Plugins
    Percona Device Packages (no support)
    Interesting Device Packages


    For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
    Post Reply

    Who is online

    Users browsing this forum: No registered users and 3 guests