ServerCheck : Get the Sensor value from CLI


Background

I'm monitoring the temperature in the datacenter with sensors from ServerCheck. They are reliable and very efficient. They support SNMP and have a comprehensive UI. But, I recently had the need to get the value of one sensor from the CLI. I noticed the company has been renamed to InfraSensing, but the hardware is still the same. Let's see what can be done !

API ?

I first thought there are some APIs I can call. But ... not exactly. After doing some researches, I manage to find a hidden URL that returns a very long string formatted in XML with all the relevant details.

The problem is, it appears to be a different URL with different versions. So, here are the possibilities : 

http://<sensor_ip>/retcurvalue.xml

or

http://<sensor_ip>/xmlOutput.xml

I have no precisions about what version support what URL, just give it a try. What I know is : I'm running Release 8.6 (from Jan 6 2021) and I need to use the second URL.

It will produce something like this (Chrome did the XML parsing and display it as a tree structure) :

<note>
<status>
<from>192.168.x.x</from>
<from>sensor1</from>
<ssname0>Temp back racks</ssname0>
<ssvalue0>27.38</ssvalue0>
<ssname1>Flooding1</ssname1>
<ssvalue1>0.00</ssvalue1>
<ssname2>Temp front racks</ssname2>
<ssvalue2>24.44</ssvalue2>
<ssname3>Humidity1</ssname3>
<ssvalue3>37.50</ssvalue3>
<ssname4>Dew Point1</ssname4>
<ssvalue4>13.94</ssvalue4>
</status>
<alerts>
<alert1>Temp r1,26.97,NORMAL,08 February 2023,15:50:17</alert1>
[...]

So, once you have this, that's only a matter of extracting the right information. It always been a pleasure to do that with php !

The below example is quick and dirty, but I'm sure you'll have an idea : 

<?php
$sensorGateway="<your_sensor_gateway_ip>";
$login="<your_login>";
$password="<your_password>";
$url="xmlOutput.xml";

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $login.":".$password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_URL, "http://".$sensorGateway."/".$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$result=simplexml_load_string($result);

$currDate=date('Y-m-d H:i:s');
print("Current Date time : ".$currDate."\n");
print("Sensor 1 Temp : ".$result->status->ssvalue0." °C\n");
print("Sensor 2 Temp : ".$result->status->ssvalue2." °C\n");
?>

This will output the value for sensor0 and sensor2 : 

Current Date time : 2023-02-17 11:51:12
Sensor 1 Temp : 25.39 °C
Sensor 2 Temp : 26.62 °C

Now, you can improve the script in 2 ways : 
  • Create a function and pass the arguments;
  • Log the data on a file to get the trends.
Here is the function : 

function scGetSensor($sensorConnect,$sensorIdx)
{
    $url="xmlOutput.xml";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_USERPWD, $sensorConnect["username"].":".$sensorConnect["password"]);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_URL, "http://".$sensorConnect["ip"]."/".$url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    $result=simplexml_load_string($result);
    // Build the property name according to the senssor IDX
    $property="ssvalue".$sensorIdx;
    $tmp=$result->status->{$property};

    return $tmp;
}

You probably notice I'm using a $sensorConnect array : 

$sensorConnect=array(
    "ip" => "<your_sensor_gateway_ip>",
    "username" => "<your_login>",
    "password" => "<your_password>"
    );  

Simply call the function this way : 

$sensor1=scGetSensor($sensorConnect,"0");

If you wish to log the data to a file, you can use this trick : 

$fp = fopen($output, 'a');
fwrite($fp, $currDate.','.$result->status->ssvalue0.','.$result->status->ssvalue2."\n"); 
fclose($fp);  

This will create a file called $output, if the file already exists, it will append the new data at the end.

I think it's always easier and more satisfying to extract the data by yourself since you know exactly how to deal with them by controlling the output format. In my use case, SNMP was available but I needed to open some requests to have firewall opened. It does not worth it since I needed to monitor the progress over 24 hours. But as always, Php came to the rescue.

I hope this helps anyone else ! ;)



Comments

What's hot ?

Raspberry Pi : WiFi Hotspot for the Garden!

ShredOS : HDD degaussing with style

Wallbox : Get The Most Of It (with API)