Librenms/Weathermap changer la couleur de l'icone en fonction du status de l'equipement.

Le plugin weathermap pour LibreNMS est trés utile pour creer des maps.

Voici un petit tuto pour faire en sorte que l'icone d'un node change en fonction de l'etat d'un équipement.

nous utiliserons les datasources de Weathermap et l'API de LibreNMS.

Etape 1 : Creation d'un nouveau fichier de datasource

Dans /Weathermap/lib/datasources creez le fichier WeatherMapDataSource_libreAPI.php et inscrivez ceci : 

<?php
// Pluggable datasource for PHP Weathermap 0.9
// - Query Librenms API for device status
//
// must have jq & curl installed

// in .conf do
//
// 	SCALE updown 0 0  255 0 0
// 	SCALE updown 0.5 1 255 255 255
//
// under a node do
// 	USESCALE updown out
//  TARGET libreAPI:hostname

class WeatherMapDataSource_libreAPI extends WeatherMapDataSource {

private $curl_cmd;

	function Init(&$map)
	{
		$this->curl_cmd = "/usr/bin/curl";
		return(TRUE);
	}

	function Recognise($targetstring)
	{
		if(preg_match("/^libreAPI:(S+)$/",$targetstring,$matches))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function ReadData($targetstring, &$map, &$item)
	{
		//created and set via http://librenms/api-access
		$weatherapikey = "fc0b71a108c999bf2f4a889f053b24bb";
		$librenmsurl = "http://librenms";
		//set the above to match your env
		$data[IN] = NULL;
		$data[OUT] = NULL;
		if(preg_match("/^libreAPI:(S+)$/",$targetstring,$matches))
		{
			$target = $matches[1];
			if(is_executable($this->curl_cmd))
			{
				$command = $this->curl_cmd." -s -H X-Auth-Token:$weatherapikey $librenmsurl/api/v0/devices/$target | jq '.devices[].status'";
				wm_debug("Running $commandn");
				$pipe=popen($command, "r");
				echo "'$pipe'; " . gettype($pipe) . "n";
				$data[OUT] = fread($pipe, 2096);
				echo $pipe;
				pclose($pipe);
				//convert the true / flase into int for scale
				$data[OUT] = (int) filter_var($data[OUT], FILTER_VALIDATE_BOOLEAN);
				}

			}
			wm_debug ("ReadData: Returning = ".($data[OUT]===NULL?'NULL':$data[OUT])."n");
			return( array($data[IN], $data[OUT]) );
		}
	}


// vim:ts=4:sw=4:

Dans ce fichier vous aurez à renseigner correctement les lignes : 

$weatherapikey = "fc0b71a108c999bf2f4a889f053b24bb";
$librenmsurl = "http://librenms";

Il y a 2 methodes.

Methode 1 : 

Dans le fichier de conf de la map ajoutez

SCALE updown 1    1    102 204   0  router_up.gif
SCALE updown 0    0    255   0   0  router_down.gif

Si l'etat de l'equipement est à "0" l'image routeur_down.gif sera utilisé

Si l'etat de l'équipement est à "1" l'image router_up.gif sera utilisé

Dans la configuration du node:

ICON images/{node:this:inscaletag}
TARGET libreAPI:192.168.0.123
USESCALE updown out percent
USEICONSCALE updown out

Methode 2 : 

Dans le fichier de conf de la map : 

SCALE updown 1    1    102 204   0  up
SCALE updown 0    0    255   0   0  down

Ici je ne renseigne pas directement l'image à utiliser mais seulement les notions de "up" et "down"

Dans la configuration du node : 

ICON images/router_{node:this:inscaletag}.gif
	TARGET libreAPI:192.168.0.123
	USESCALE updown out percent
	USEICONSCALE updown out

L'approche est un peu differente, à vous de voir ce qui vous va le mieux.

 

 

Écrire un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec une *

Quelle est le cinquième caractère du mot kjeq9p8l ? :