From 74f89978fc7941f5a162814fadb5b58f6fc676d1 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Fri, 14 Oct 2016 18:14:18 -0500 Subject: [PATCH] refactor: toner support (#4795) --- includes/discovery/functions.inc.php | 66 +++----------------- includes/discovery/toner.inc.php | 92 ++++++++++++---------------- includes/functions.php | 41 +++++++++++++ includes/polling/toner.inc.php | 13 ++-- 4 files changed, 94 insertions(+), 118 deletions(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 85a4edf067..530331f09a 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -897,66 +897,14 @@ function get_device_divisor($device, $serial, $sensor) } /** - * @param $device - * @param $capacity_oid - * @return int + * @param int $raw_capacity The value return from snmp + * @return int normalized capacity value */ -function get_toner_capacity($device, $capacity_oid) +function get_toner_capacity($raw_capacity) { - if ($device['os'] == 'ricoh' || $device['os'] == 'nrg' || $device['os'] == 'lanier') { - $capacity = 100; - } else { - $capacity = snmp_get($device, $capacity_oid, '-Oqv'); + // unknown or unrestricted capacity, assume 100 + if (empty($raw_capacity) || $raw_capacity < 0) { + return 100; } - - return $capacity; -} - -/** - * @param $device - * @param $oid_value - * @param $oid_capacity - * @return float|int - */ -function get_toner_levels($device, $oid_value, $oid_capacity) -{ - if ($device['os'] == 'ricoh' || $device['os'] == 'nrg' || $device['os'] == 'lanier') { - if ($oid_value == '-3') { - $current = 50; - } elseif ($oid_value == '-100') { - $current = 0; - } else { - $current = ($oid_value / $oid_capacity * 100); - } - } elseif ($device['os'] == 'brother') { - if (str_contains($device['hardware'], 'NC-8600h')) { - switch ($oid_value) { - case '0': - $current = 0; - break; - case '-3': - $current = 50; - break; - } - } else { - switch ($oid_value) { - case '0': - $current = 100; - break; - case '1': - $current = 5; - break; - case '2': - $current = 0; - break; - case '3': - $current = 1; - break; - } - } - } else { - $current = ($oid_value / $oid_capacity * 100); - } - - return $current; + return $raw_capacity; } diff --git a/includes/discovery/toner.inc.php b/includes/discovery/toner.inc.php index 1418653d06..827e706d97 100644 --- a/includes/discovery/toner.inc.php +++ b/includes/discovery/toner.inc.php @@ -3,69 +3,55 @@ $valid_toner = array(); if ($device['os_group'] == 'printer') { - $oids = trim(snmp_walk($device, 'SNMPv2-SMI::mib-2.43.12.1.1.2.1 ', '-OsqnU')); - if (!$oids) { - $oids = trim(snmp_walk($device, 'SNMPv2-SMI::mib-2.43.11.1.1.2.1 ', '-OsqnU')); + echo 'Toner: '; + $oids = snmpwalk_cache_oid($device, 'prtMarkerColorantMarkerIndex', array(), 'Printer-MIB'); + if (empty($oids)) { + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesMarkerIndex', $oids, 'Printer-MIB'); } - d_echo($oids . "\n"); - - if ($oids) { - echo 'Jetdirect '; + if (!empty($oids)) { + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesLevel', $oids, 'Printer-MIB'); + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesMaxCapacity', $oids, 'Printer-MIB'); + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesDescription', $oids, 'Printer-MIB', null, '-OQUsa'); } - //samsung ml series does not responde to snmpwalk - if ($os == 'samsungprinter' && str_contains($device['sysDescr'], 'Samsung ML')) { - $toner_oid = '.1.3.6.1.2.1.43.11.1.1.9.1.1'; - $descr_oid = '.1.3.6.1.2.1.43.11.1.1.6.1.1'; - $capacity_oid = '.1.3.6.1.2.1.43.11.1.1.8.1.1'; - $descr = trim(str_replace("\n", '', preg_replace('/[^ \w]+/', '', snmp_get($device, $descr_oid, '-Oqv')))); - $oid_toner = snmp_get($device, $toner_oid, '-Oqv'); - $oid_capacity = snmp_get($device, $capacity_oid, '-Oqv'); - $capacity = get_toner_capacity($device, $oid_capacity); - $current = get_toner_levels($device, $oid_toner, $oid_capacity); + foreach ($oids as $index => $data) { + $last_index = substr($index, strrpos($index, '.') + 1); + if ($os == 'ricoh' || $os == 'nrg' || $os == 'lanier') { + $toner_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.$last_index"; + $descr_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.3.$last_index"; + $capacity_oid = ''; - discover_toner($valid_toner, $device, $toner_oid, 1, 'jetdirect', $descr, $capacity_oid, $capacity, $current); - } else { - foreach (explode("\n", $oids) as $data) { - $data = trim($data); - if ($data) { - list($oid, $role) = explode(' ', $data); - $split_oid = explode('.', $oid); - $index = $split_oid[(count($split_oid) - 1)]; - if (is_numeric($role)) { - //ricoh using private oids to expose toner levels - if ($os == 'ricoh' || $os == 'nrg' || $os == 'lanier') { - $toner_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.$index"; - $descr_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.3.$index"; - $capacity_oid = ''; - } else { - $toner_oid = ".1.3.6.1.2.1.43.11.1.1.9.1.$index"; - $descr_oid = ".1.3.6.1.2.1.43.11.1.1.6.1.$index"; - $capacity_oid = ".1.3.6.1.2.1.43.11.1.1.8.1.$index"; - } + $descr = snmp_get($device, $descr_oid, '-Oqva'); + $raw_toner = snmp_get($device, $toner_oid, '-Oqv'); + } else { + $toner_oid = ".1.3.6.1.2.1.43.11.1.1.9.$index"; + $capacity_oid = ".1.3.6.1.2.1.43.11.1.1.8.$index"; - $descr = trim(str_replace("\n", '', preg_replace('/[^ \w]+/', '', snmp_get($device, $descr_oid, '-Oqva')))); - - if ($descr != '') { - $oid_toner = snmp_get($device, $toner_oid, '-Oqv'); - $oid_capacity = snmp_get($device, $capacity_oid, '-Oqv'); - - $capacity = get_toner_capacity($device, $oid_capacity); - $current = get_toner_levels($device, $oid_toner, $oid_capacity); - - $type = 'jetdirect'; - - discover_toner($valid_toner, $device, $toner_oid, $index, $type, $descr, $capacity_oid, $capacity, $current); - } - } - } + $descr = $data['prtMarkerSuppliesDescription']; + $raw_toner = $data['prtMarkerSuppliesLevel']; } + + $type = 'jetdirect'; + $capacity = get_toner_capacity($data['prtMarkerSuppliesMaxCapacity']); + $current = get_toner_levels($device, $raw_toner, $capacity); + + discover_toner( + $valid_toner, + $device, + $toner_oid, + $last_index, + $type, + $descr, + $capacity_oid, + $capacity, + $current + ); } } // Delete removed toners -d_echo("\n Checking ... \n"); +d_echo("\n Checking valid toner ... \n"); d_echo($valid_toner); $sql = "SELECT * FROM toner WHERE device_id = '" . $device['device_id'] . "'"; @@ -79,4 +65,4 @@ foreach (dbFetchRows($sql) as $test_toner) { } unset($valid_toner); -echo "\n"; +echo PHP_EOL; diff --git a/includes/functions.php b/includes/functions.php index f7b18ab2bc..73858ebc49 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1695,3 +1695,44 @@ function q_bridge_bits2indices($hex_data) } return $indices; } + +/** + * @param array $device + * @param int|string $raw_value The value returned from snmp + * @param int $capacity the normalized capacity + * @return int the toner level as a percentage + */ +function get_toner_levels($device, $raw_value, $capacity) +{ + // -3 means some toner is left + if ($raw_value == '-3') { + return 50; + } + + // -2 means unknown, -1 mean no restrictions + if ($raw_value == '-2' || $raw_value == '-1') { + return 0; // FIXME: is 0 what we should return? + } + + // Non-standard snmp values + if ($device['os'] == 'ricoh' || $device['os'] == 'nrg' || $device['os'] == 'lanier') { + if ($raw_value == '-100') { + return 0; + } + } elseif ($device['os'] == 'brother') { + if (!str_contains($device['hardware'], 'MFC-L8850')) { + switch ($raw_value) { + case '0': + return 100; + case '1': + return 5; + case '2': + return 0; + case '3': + return 1; + } + } + } + + return round($raw_value / $capacity * 100); +} diff --git a/includes/polling/toner.inc.php b/includes/polling/toner.inc.php index fbc454b53c..2882291222 100644 --- a/includes/polling/toner.inc.php +++ b/includes/polling/toner.inc.php @@ -5,11 +5,8 @@ $toner_data = dbFetchRows('SELECT * FROM toner WHERE device_id = ?', array($devi foreach ($toner_data as $toner) { echo 'Checking toner '.$toner['toner_descr'].'... '; - if ($toner['toner_capacity_oid']) { - $toner['toner_capacity'] = snmp_get($device, $toner['toner_capacity_oid'], '-OUqnv'); - } - - $tonerperc = round((snmp_get($device, $toner['toner_oid'], '-OUqnv') / $toner['toner_capacity'] * 100)); + $raw_toner = snmp_get($device, $toner['toner_oid'], '-OUqnv'); + $tonerperc = get_toner_levels($device, $raw_toner, $toner['toner_capacity']); echo $tonerperc." %\n"; $tags = array( @@ -20,7 +17,11 @@ foreach ($toner_data as $toner) { ); data_update($device, 'toner', $tags, $tonerperc); - // FIXME should report for toner out... :) + // Log empty supplies (but only once) + if ($tonerperc == 0 && $toner['toner_current'] > 0) { + log_event('Toner '.$toner['toner_descr'].' is empty', $device, 'toner', $toner['toner_id']); + } + // Log toner swap if ($tonerperc > $toner['toner_current']) { log_event('Toner '.$toner['toner_descr'].' was replaced (new level: '.$tonerperc.'%)', $device, 'toner', $toner['toner_id']);