diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index 6a7e633ebe..acf62316e1 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -432,6 +432,8 @@ class Config self::deprecatedVariable('rrdgraph_real_95th', 'rrdgraph_real_percentile'); self::deprecatedVariable('fping_options.millisec', 'fping_options.interval'); self::deprecatedVariable('discovery_modules.cisco-vrf', 'discovery_modules.vrf'); + self::deprecatedVariable('discovery_modules.toner', 'discovery_modules.printer-supplies'); + self::deprecatedVariable('poller_modules.toner', 'poller_modules.printer-supplies'); self::deprecatedVariable('oxidized.group', 'oxidized.maps.group'); $persist = Eloquent::isConnected(); diff --git a/LibreNMS/Modules/PrinterSupplies.php b/LibreNMS/Modules/PrinterSupplies.php new file mode 100644 index 0000000000..ed6ddfaccd --- /dev/null +++ b/LibreNMS/Modules/PrinterSupplies.php @@ -0,0 +1,282 @@ +. + * + * @link https://www.librenms.org + */ + +namespace LibreNMS\Modules; + +use App\Models\PrinterSupply; +use App\Observers\ModuleModelObserver; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; +use LibreNMS\DB\SyncsModels; +use LibreNMS\Interfaces\Module; +use LibreNMS\OS; +use LibreNMS\RRD\RrdDefinition; +use Log; + +class PrinterSupplies implements Module +{ + use SyncsModels; + + /** + * Discover this module. Heavier processes can be run here + * Run infrequently (default 4 times a day) + * + * @param OS $os + */ + public function discover(OS $os) + { + $device = $os->getDeviceArray(); + + $data = collect() + ->concat($this->discoveryLevels($device)) + ->concat($this->discoveryPapers($device)); + + ModuleModelObserver::observe(PrinterSupply::class); + $this->syncModels($os->getDevice(), 'printerSupplies', $data); + } + + /** + * Poll data for this module and update the DB / RRD. + * Try to keep this efficient and only run if discovery has indicated there is a reason to run. + * Run frequently (default every 5 minutes) + * + * @param OS $os + */ + public function poll(OS $os) + { + $device = $os->getDeviceArray(); + $toner_data = $os->getDevice()->printerSupplies; + + $toner_snmp = snmp_get_multi_oid($device, $toner_data->pluck('supply_oid')->toArray()); + + foreach ($toner_data as $toner) { + echo 'Checking toner ' . $toner['supply_descr'] . '... '; + + $raw_toner = $toner_snmp[$toner['supply_oid']]; + $tonerperc = self::getTonerLevel($device, $raw_toner, $toner['supply_capacity']); + echo $tonerperc . " %\n"; + + $tags = [ + 'rrd_def' => RrdDefinition::make()->addDataset('toner', 'GAUGE', 0, 20000), + 'rrd_name' => ['toner', $toner['supply_index']], + 'rrd_oldname' => ['toner', $toner['supply_descr']], + 'index' => $toner['supply_index'], + ]; + data_update($device, 'toner', $tags, $tonerperc); + + // Log empty supplies (but only once) + if ($tonerperc == 0 && $toner['supply_current'] > 0) { + Log::event('Toner ' . $toner['supply_descr'] . ' is empty', $device, 'toner', 5, $toner['supply_id']); + } + + // Log toner swap + if ($tonerperc > $toner['supply_current']) { + Log::event('Toner ' . $toner['supply_descr'] . ' was replaced (new level: ' . $tonerperc . '%)', $device, 'toner', 3, $toner['supply_id']); + } + + $toner->supply_current = $tonerperc; + $toner->supply_capacity = $toner['supply_capacity']; + $toner->save(); + } + } + + /** + * Remove all DB data for this module. + * This will be run when the module is disabled. + * + * @param OS $os + */ + public function cleanup(OS $os) + { + $os->getDevice()->printerSupplies()->delete(); + } + + private function discoveryLevels($device): Collection + { + $levels = collect(); + + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesLevel', [], 'Printer-MIB'); + if (! empty($oids)) { + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesType', $oids, 'Printer-MIB'); + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesMaxCapacity', $oids, 'Printer-MIB'); + $oids = snmpwalk_cache_oid($device, 'prtMarkerSuppliesDescription', $oids, 'Printer-MIB', null, '-OQUsa'); + } + + foreach ($oids as $index => $data) { + $last_index = substr($index, strrpos($index, '.') + 1); + + $descr = $data['prtMarkerSuppliesDescription']; + $raw_capacity = $data['prtMarkerSuppliesMaxCapacity']; + $raw_toner = $data['prtMarkerSuppliesLevel']; + $supply_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"; + + // work around weird HP bug where descriptions are on two lines and the second line is hex + if (Str::contains($descr, "\n")) { + $new_descr = ''; + foreach (explode("\n", $descr) as $line) { + if (preg_match('/^([A-F\d]{2} )*[A-F\d]{1,2} ?$/', $line)) { + $line = snmp_hexstring($line); + } + $new_descr .= $line; + } + $descr = trim($new_descr); + } + + // Ricoh - TONERCurLevel + if (empty($raw_toner)) { + $supply_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.$last_index"; + $raw_toner = snmp_get($device, $supply_oid, '-Oqv'); + } + + // Ricoh - TONERNameLocal + if (empty($descr)) { + $descr_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.3.$last_index"; + $descr = snmp_get($device, $descr_oid, '-Oqva'); + } + + // trim part & serial number from devices that include it + if (Str::contains($descr, ', PN')) { + $descr = explode(', PN', $descr)[0]; + } + + $capacity = self::getTonerCapacity($raw_capacity); + $current = self::getTonerLevel($device, $raw_toner, $capacity); + + if (is_numeric($current)) { + $levels->push(new PrinterSupply([ + 'device_id' => $device['device_id'], + 'supply_oid' => $supply_oid, + 'supply_capacity_oid' => $capacity_oid, + 'supply_index' => $last_index, + 'supply_type' => $data['prtMarkerSuppliesType'] ?: 'markerSupply', + 'supply_descr' => $descr, + 'supply_capacity' => $capacity, + 'supply_current' => $current, + ])); + } + } + + return $levels; + } + + private function discoveryPapers($device): Collection + { + echo 'Tray Paper Level: '; + $papers = collect(); + + $tray_oids = snmpwalk_cache_oid($device, 'prtInputName', [], 'Printer-MIB'); + if (! empty($tray_oids)) { + $tray_oids = snmpwalk_cache_oid($device, 'prtInputCurrentLevel', $tray_oids, 'Printer-MIB'); + $tray_oids = snmpwalk_cache_oid($device, 'prtInputMaxCapacity', $tray_oids, 'Printer-MIB'); + } + + foreach ($tray_oids as $index => $data) { + $last_index = substr($index, strrpos($index, '.') + 1); + + $capacity = $data['prtInputMaxCapacity']; + $current = $data['prtInputCurrentLevel']; + + if (! is_numeric($current) || $current == -2) { + // capacity unsupported + d_echo('Input Capacity unsupported', 'X'); + continue; + } elseif ($current == -3) { + // at least one piece of paper in tray + $current = 50; + } else { + $current = $current / $capacity * 100; + } + + $papers->push(new PrinterSupply([ + 'device_id' => $device['device_id'], + 'supply_oid' => ".1.3.6.1.2.1.43.8.2.1.10.$index", + 'supply_capacity_oid' => ".1.3.6.1.2.1.43.8.2.1.9.$index", + 'supply_index' => $last_index, + 'supply_type' => 'input', + 'supply_descr' => $data['prtInputName'], + 'supply_capacity' => $capacity, + 'supply_current' => $current, + ])); + } + + return $papers; + } + + /** + * @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 + */ + private static function getTonerLevel($device, $raw_value, $capacity) + { + // -3 means some toner is left + if ($raw_value == '-3') { + return 50; + } + + // -2 means unknown + if ($raw_value == '-2') { + return false; + } + + // -1 mean no restrictions + if ($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); + } + + /** + * @param int $raw_capacity The value return from snmp + * @return int normalized capacity value + */ + private static function getTonerCapacity($raw_capacity) + { + // unknown or unrestricted capacity, assume 100 + if (empty($raw_capacity) || $raw_capacity < 0) { + return 100; + } + + return $raw_capacity; + } +} diff --git a/LibreNMS/Util/ObjectCache.php b/LibreNMS/Util/ObjectCache.php index 70042921cd..e93501d37b 100644 --- a/LibreNMS/Util/ObjectCache.php +++ b/LibreNMS/Util/ObjectCache.php @@ -32,10 +32,10 @@ use App\Models\Device; use App\Models\Mpls; use App\Models\OspfInstance; use App\Models\Port; +use App\Models\PrinterSupply; use App\Models\Pseudowire; use App\Models\Sensor; use App\Models\Service; -use App\Models\Toner; use App\Models\Vrf; use Cache; @@ -98,7 +98,7 @@ class ObjectCache ]; } - if (Toner::hasAccess(auth()->user())->exists()) { + if (PrinterSupply::hasAccess(auth()->user())->exists()) { $sensor_menu[3] = [ [ 'class' => 'toner', diff --git a/app/Http/Controllers/AboutController.php b/app/Http/Controllers/AboutController.php index bca39c561e..d86511701a 100644 --- a/app/Http/Controllers/AboutController.php +++ b/app/Http/Controllers/AboutController.php @@ -38,13 +38,13 @@ use App\Models\Ipv6Address; use App\Models\Ipv6Network; use App\Models\Mempool; use App\Models\Port; +use App\Models\PrinterSupply; use App\Models\Processor; use App\Models\Pseudowire; use App\Models\Sensor; use App\Models\Service; use App\Models\Storage; use App\Models\Syslog; -use App\Models\Toner; use App\Models\Vlan; use App\Models\Vrf; use App\Models\WirelessSensor; @@ -98,7 +98,7 @@ class AboutController extends Controller 'stat_services' => Service::count(), 'stat_storage' => Storage::count(), 'stat_syslog' => Syslog::count(), - 'stat_toner' => Toner::count(), + 'stat_toner' => PrinterSupply::count(), 'stat_vlans' => Vlan::count(), 'stat_vrf' => Vrf::count(), 'stat_wireless' => WirelessSensor::count(), diff --git a/app/Models/Device.php b/app/Models/Device.php index 44830aeb7e..231883ba1f 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -762,7 +762,7 @@ class Device extends BaseModel public function printerSupplies() { - return $this->hasMany(Toner::class, 'device_id'); + return $this->hasMany(PrinterSupply::class, 'device_id'); } public function pseudowires() diff --git a/app/Models/PrinterSupply.php b/app/Models/PrinterSupply.php new file mode 100644 index 0000000000..e1b01bbeba --- /dev/null +++ b/app/Models/PrinterSupply.php @@ -0,0 +1,25 @@ +supply_type-$this->supply_index"; + } +} diff --git a/app/Models/Toner.php b/app/Models/Toner.php deleted file mode 100644 index d33c609ffa..0000000000 --- a/app/Models/Toner.php +++ /dev/null @@ -1,10 +0,0 @@ -renameColumn('toner_id', 'supply_id'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_index', 'supply_index'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_type', 'supply_type'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_oid', 'supply_oid'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_descr', 'supply_descr'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_capacity', 'supply_capacity'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_current', 'supply_current'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('toner_capacity_oid', 'supply_capacity_oid'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->string('supply_descr', 255)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_id', 'toner_id'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_index', 'toner_index'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_type', 'toner_type'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_oid', 'toner_oid'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_descr', 'toner_descr'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_capacity', 'toner_capacity'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_current', 'toner_current'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->renameColumn('supply_capacity_oid', 'toner_capacity_oid'); + }); + + Schema::table('printer_supplies', function (Blueprint $table) { + $table->string('toner_descr', 32)->change(); + }); + } +} diff --git a/includes/caches/toner.inc.php b/includes/caches/toner.inc.php index a66067b6f1..8c65de9ced 100644 --- a/includes/caches/toner.inc.php +++ b/includes/caches/toner.inc.php @@ -1,13 +1,13 @@ hasGlobalRead()) { - $data['count'] = ['query' => 'SELECT COUNT(`toner_id`) FROM toner']; + $data['count'] = ['query' => 'SELECT COUNT(`supply_id`) FROM printer_supplies']; } else { $device_ids = Permissions::devicesForUser()->toArray() ?: [0]; - $perms_sql = '`toner`.`device_id` IN ' . dbGenPlaceholders(count($device_ids)); + $perms_sql = '`printer_supplies`.`device_id` IN ' . dbGenPlaceholders(count($device_ids)); $data['count'] = [ - 'query' => "SELECT COUNT(`toner_id`) FROM toner WHERE $perms_sql", + 'query' => "SELECT COUNT(`supply_id`) FROM printer_supplies WHERE $perms_sql", 'params' => $device_ids, ]; } diff --git a/includes/callback.php b/includes/callback.php index abebd7c87a..2f62b3f17f 100644 --- a/includes/callback.php +++ b/includes/callback.php @@ -57,7 +57,7 @@ if ($enabled == 1) { 'sensors' => 'SELECT COUNT(*) AS `total`,`sensor_class` FROM `sensors` GROUP BY `sensor_class`', 'wireless' => 'SELECT COUNT(*) AS `total`,`sensor_class` FROM `wireless_sensors` GROUP BY `sensor_class`', 'storage' => 'SELECT COUNT(*) AS `total`,`storage_type` FROM `storage` GROUP BY `storage_type`', - 'toner' => 'SELECT COUNT(*) AS `total`,`toner_type` FROM `toner` GROUP BY `toner_type`', + 'toner' => 'SELECT COUNT(*) AS `total`,`supply_type` FROM `printer_supplies` GROUP BY `supply_type`', 'vlans' => 'SELECT COUNT(*) AS `total`,`vlan_type` FROM `vlans` GROUP BY `vlan_type`', 'vminfo' => 'SELECT COUNT(*) AS `total`,`vm_type` FROM `vminfo` GROUP BY `vm_type`', 'vmware' => 'SELECT COUNT(*) AS `total` FROM `vminfo`', diff --git a/includes/definitions/brother.yaml b/includes/definitions/brother.yaml index 92a4ee4f65..4f6f96a2d1 100644 --- a/includes/definitions/brother.yaml +++ b/includes/definitions/brother.yaml @@ -9,6 +9,6 @@ discovery: - sysDescr_regex: '/Brother NC-.*(h|w),/' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/canonprinter.yaml b/includes/definitions/canonprinter.yaml index bdb31538c4..895802b223 100644 --- a/includes/definitions/canonprinter.yaml +++ b/includes/definitions/canonprinter.yaml @@ -16,6 +16,6 @@ discovery: - .1.3.6.1.4.1.1602.4.2 - .1.3.6.1.4.1.1602.4.7 discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/dell-laser.yaml b/includes/definitions/dell-laser.yaml index 9f6a21aee0..ff67eb9cd1 100644 --- a/includes/definitions/dell-laser.yaml +++ b/includes/definitions/dell-laser.yaml @@ -17,6 +17,6 @@ discovery: - sysObjectID: .1.3.6.1.4.1.674.10898. discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/developprinter.yaml b/includes/definitions/developprinter.yaml index b2fd0ed210..c6ef0cfc50 100644 --- a/includes/definitions/developprinter.yaml +++ b/includes/definitions/developprinter.yaml @@ -10,6 +10,6 @@ discovery: sysObjectID: - .1.3.6.1.4.1.18334.1.2.1.2.1.50.2.2 discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/epson.yaml b/includes/definitions/epson.yaml index 2349a1779c..875e33e323 100644 --- a/includes/definitions/epson.yaml +++ b/includes/definitions/epson.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - 'EPSON Built-in' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/gestetner.yaml b/includes/definitions/gestetner.yaml index aaf250679e..310bb2e56c 100644 --- a/includes/definitions/gestetner.yaml +++ b/includes/definitions/gestetner.yaml @@ -10,8 +10,8 @@ discovery: sysDescr: - Gestetner Network Printer discovery_modules: - toner: true + printer-supplies: true mempools: false poller_modules: - toner: true + printer-supplies: true mempools: false diff --git a/includes/definitions/jetdirect.yaml b/includes/definitions/jetdirect.yaml index 44227b8bfc..3a09cd75a5 100644 --- a/includes/definitions/jetdirect.yaml +++ b/includes/definitions/jetdirect.yaml @@ -7,9 +7,9 @@ icon: hp over: - { graph: device_toner, text: Toner } discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true discovery: - sysDescr: diff --git a/includes/definitions/konica.yaml b/includes/definitions/konica.yaml index 4f7e224f72..ab46f9b316 100644 --- a/includes/definitions/konica.yaml +++ b/includes/definitions/konica.yaml @@ -13,6 +13,6 @@ discovery: sysObjectID: - .1.3.6.1.4.1.18334.1. discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/kyocera.yaml b/includes/definitions/kyocera.yaml index f22c685a8b..74a826fe20 100644 --- a/includes/definitions/kyocera.yaml +++ b/includes/definitions/kyocera.yaml @@ -10,6 +10,6 @@ discovery: sysDescr_regex: - '/^KYOCERA/' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/lanier.yaml b/includes/definitions/lanier.yaml index 3efa7e9c1f..160329675b 100644 --- a/includes/definitions/lanier.yaml +++ b/includes/definitions/lanier.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - LANIER discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/lexmarkprinter.yaml b/includes/definitions/lexmarkprinter.yaml index 187df9bc06..d3b84a40b1 100644 --- a/includes/definitions/lexmarkprinter.yaml +++ b/includes/definitions/lexmarkprinter.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - Lexmark discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/nrg.yaml b/includes/definitions/nrg.yaml index d2bb5fbf58..e7c99e7e21 100644 --- a/includes/definitions/nrg.yaml +++ b/includes/definitions/nrg.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - 'NRG Network Printer' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/okilan.yaml b/includes/definitions/okilan.yaml index a3c1ee9bb8..88c3e37779 100644 --- a/includes/definitions/okilan.yaml +++ b/includes/definitions/okilan.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - 'OKI OkiLAN' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/ricoh.yaml b/includes/definitions/ricoh.yaml index 9354bf3647..80e9990fa8 100644 --- a/includes/definitions/ricoh.yaml +++ b/includes/definitions/ricoh.yaml @@ -11,6 +11,6 @@ discovery: - 'RICOH Aficio' - 'RICOH Network Printer' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/samsungprinter.yaml b/includes/definitions/samsungprinter.yaml index 0ee325104f..cc00b35f48 100644 --- a/includes/definitions/samsungprinter.yaml +++ b/includes/definitions/samsungprinter.yaml @@ -11,6 +11,6 @@ discovery: - 'Samsung S' - 'Samsung ML' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/savin.yaml b/includes/definitions/savin.yaml index d903ae5572..04fbbd0d2e 100644 --- a/includes/definitions/savin.yaml +++ b/includes/definitions/savin.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - SAVIN discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/sharp.yaml b/includes/definitions/sharp.yaml index 364bf6e39d..ba76b85894 100644 --- a/includes/definitions/sharp.yaml +++ b/includes/definitions/sharp.yaml @@ -10,6 +10,6 @@ discovery: sysDescr: - 'SHARP MX-' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/toshiba-tec.yaml b/includes/definitions/toshiba-tec.yaml index 121a3990c9..5a017ce0e1 100644 --- a/includes/definitions/toshiba-tec.yaml +++ b/includes/definitions/toshiba-tec.yaml @@ -14,6 +14,6 @@ discovery: sysDescr: - 'TOSHIBA e-STUDIO' discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/definitions/waas.yaml b/includes/definitions/waas.yaml index 45271b7a5e..e865e63934 100644 --- a/includes/definitions/waas.yaml +++ b/includes/definitions/waas.yaml @@ -21,7 +21,7 @@ poller_modules: cisco-voice: false ipmi: false ospf: false - toner: false + printer-supplies: false ucd-diskio: false ucd-mib: false discovery_modules: @@ -36,7 +36,7 @@ discovery_modules: libvirt-vminfo: false ntp: false stp: false - toner: false + printer-supplies: false ucd-diskio: false vlans: false discovery: diff --git a/includes/definitions/xerox.yaml b/includes/definitions/xerox.yaml index 803e6d2f29..bf2ef1bc7a 100644 --- a/includes/definitions/xerox.yaml +++ b/includes/definitions/xerox.yaml @@ -11,6 +11,6 @@ discovery: - .1.3.6.1.4.1.253.8.62.1. - .1.3.6.1.4.1.297.1.11.93.1. discovery_modules: - toner: true + printer-supplies: true poller_modules: - toner: true + printer-supplies: true diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index aab0ae29a8..7b7010b941 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -598,29 +598,6 @@ function discover_storage(&$valid, $device, $index, $type, $mib, $descr, $size, }//end if } -function discover_toner(&$valid, $device, $oid, $index, $type, $descr, $capacity_oid = null, $capacity = null, $current = null) -{ - d_echo("Discover Toner: $oid, $index, $type, $descr, $capacity_oid, $capacity, $current\n"); - - if (dbFetchCell('SELECT COUNT(toner_id) FROM `toner` WHERE device_id = ? AND toner_type = ? AND `toner_index` = ? AND `toner_oid` =?', [$device['device_id'], $type, $index, $oid]) == '0') { - $inserted = dbInsert(['device_id' => $device['device_id'], 'toner_oid' => $oid, 'toner_capacity_oid' => $capacity_oid, 'toner_index' => $index, 'toner_type' => $type, 'toner_descr' => $descr, 'toner_capacity' => $capacity, 'toner_current' => $current], 'toner'); - echo '+'; - log_event('Toner added: type ' . $type . ' index ' . $index . ' descr ' . $descr, $device, 'toner', 3, $inserted); - } else { - $toner_entry = dbFetchRow('SELECT * FROM `toner` WHERE `device_id` = ? AND `toner_type` = ? AND `toner_index` =?', [$device['device_id'], $type, $index]); - if ($oid == $toner_entry['toner_oid'] && $descr == $toner_entry['toner_descr'] && $capacity == $toner_entry['toner_capacity'] && $capacity_oid == $toner_entry['toner_capacity_oid']) { - echo '.'; - } else { - dbUpdate(['toner_descr' => $descr, 'toner_oid' => $oid, 'toner_capacity_oid' => $capacity_oid, 'toner_capacity' => $capacity], 'toner', 'device_id=? AND toner_type=? AND `toner_index`=?', [$device['device_id'], $type, $index]); - echo 'U'; - } - } - - $valid[$type][$oid] = 1; -} - -//end discover_toner() - function discover_entity_physical(&$valid, $device, $entPhysicalIndex, $entPhysicalDescr, $entPhysicalClass, $entPhysicalName, $entPhysicalModelName, $entPhysicalSerialNum, $entPhysicalContainedIn, $entPhysicalMfgName, $entPhysicalParentRelPos, $entPhysicalVendorType, $entPhysicalHardwareRev, $entPhysicalFirmwareRev, $entPhysicalSoftwareRev, $entPhysicalIsFRU, $entPhysicalAlias, $entPhysicalAssetID, $ifIndex) { d_echo("Discover Inventory Item: $entPhysicalIndex, $entPhysicalDescr, $entPhysicalClass, $entPhysicalName, $entPhysicalModelName, $entPhysicalSerialNum, $entPhysicalContainedIn, $entPhysicalMfgName, $entPhysicalParentRelPos, $entPhysicalVendorType, $entPhysicalHardwareRev, $entPhysicalFirmwareRev, $entPhysicalSoftwareRev, $entPhysicalIsFRU, $entPhysicalAlias, $entPhysicalAssetID, $ifIndex\n"); @@ -843,20 +820,6 @@ function get_device_divisor($device, $os_version, $sensor_type, $oid) return 10; } -/** - * @param int $raw_capacity The value return from snmp - * @return int normalized capacity value - */ -function get_toner_capacity($raw_capacity) -{ - // unknown or unrestricted capacity, assume 100 - if (empty($raw_capacity) || $raw_capacity < 0) { - return 100; - } - - return $raw_capacity; -} - /** * Should we ignore this storage device based on teh description? (usually the mount path or drive) * diff --git a/includes/discovery/printer-supplies.inc.php b/includes/discovery/printer-supplies.inc.php new file mode 100644 index 0000000000..7ec04732d6 --- /dev/null +++ b/includes/discovery/printer-supplies.inc.php @@ -0,0 +1,8 @@ +discover($os); diff --git a/includes/discovery/toner.inc.php b/includes/discovery/toner.inc.php deleted file mode 100644 index 17a83bf3fe..0000000000 --- a/includes/discovery/toner.inc.php +++ /dev/null @@ -1,132 +0,0 @@ - $data) { - $last_index = substr($index, strrpos($index, '.') + 1); - - $raw_toner = $data['prtMarkerSuppliesLevel']; - $descr = $data['prtMarkerSuppliesDescription']; - - // work around weird HP bug where descriptions are on two lines and the second line is hex - if (Str::contains($descr, "\n")) { - $new_descr = ''; - foreach (explode("\n", $descr) as $line) { - if (preg_match('/^([A-F\d]{2} )*[A-F\d]{1,2} ?$/', $line)) { - $line = snmp_hexstring($line); - } - $new_descr .= $line; - } - $descr = $new_descr; - } - - $raw_capacity = $data['prtMarkerSuppliesMaxCapacity']; - $raw_toner = $data['prtMarkerSuppliesLevel']; - $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"; - - if (empty($raw_toner)) { - $toner_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.$last_index"; - $raw_toner = snmp_get($device, $toner_oid, '-Oqv'); - } - - if (empty($raw_toner)) { - $raw_toner = snmp_get($device, $toner_oid, '-Oqv'); - } - - if (empty($descr)) { - $descr_oid = ".1.3.6.1.4.1.367.3.2.1.2.24.1.1.3.$last_index"; - $descr = snmp_get($device, $descr_oid, '-Oqva'); - } - - // trim part & serial number from devices that include it - if (Str::contains($descr, ', PN')) { - $descr = explode(', PN', $descr)[0]; - } - - $capacity = get_toner_capacity($data['prtMarkerSuppliesMaxCapacity']); - $current = get_toner_levels($device, $raw_toner, $capacity); - - if (is_numeric($current)) { - discover_toner( - $valid_toner, - $device, - $toner_oid, - $last_index, - $data['prtMarkerSuppliesType'] ?: 'markerSupply', - $descr, - $capacity_oid, - $capacity, - $current - ); - } - } - echo PHP_EOL; - - echo 'Tray Paper Level: '; - $tray_oids = snmpwalk_cache_oid($device, 'prtInputName', [], 'Printer-MIB'); - if (! empty($tray_oids)) { - $tray_oids = snmpwalk_cache_oid($device, 'prtInputCurrentLevel', $tray_oids, 'Printer-MIB'); - $tray_oids = snmpwalk_cache_oid($device, 'prtInputMaxCapacity', $tray_oids, 'Printer-MIB'); - } - - d_echo($tray_oids); - foreach ($tray_oids as $index => $data) { - $last_index = substr($index, strrpos($index, '.') + 1); - - $capacity = $data['prtInputMaxCapacity']; - $current = $data['prtInputCurrentLevel']; - if (! is_numeric($current) || $current == -2) { - // capacity unsupported - d_echo('Input Capacity unsupported', 'X'); - continue; - } elseif ($current == -3) { - // at least one piece of paper in tray - $current = 50; - } else { - $current = $current / $capacity * 100; - } - - discover_toner( - $valid_toner, - $device, - ".1.3.6.1.2.1.43.8.2.1.10.$index", - $last_index, - 'input', - $data['prtInputName'], - ".1.3.6.1.2.1.43.8.2.1.9.$index", - $capacity, - $current - ); - } -} - -// Delete removed toners -d_echo("\n Checking valid toner ... \n"); -d_echo($valid_toner); - -$toners = dbFetchRows('SELECT * FROM toner WHERE device_id = ?', [$device['device_id']]); -//d_echo($toners); -foreach ($toners as $test_toner) { - $toner_oid = $test_toner['toner_oid']; - $toner_type = $test_toner['toner_type']; - if (! $valid_toner[$toner_type][$toner_oid]) { - echo '-'; - dbDelete('toner', '`toner_id` = ?', [$test_toner['toner_id']]); - } -} - -unset($valid_toner, $line, $new_descr); -echo PHP_EOL; diff --git a/includes/functions.php b/includes/functions.php index 0c866b59a1..7360aab9fc 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1678,52 +1678,6 @@ 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 - if ($raw_value == '-2') { - return false; - } - - // -1 mean no restrictions - if ($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); -} - /** * Intialize global stat arrays */ diff --git a/includes/html/graphs/device/toner.inc.php b/includes/html/graphs/device/toner.inc.php index d55ace47a7..c105e6275a 100644 --- a/includes/html/graphs/device/toner.inc.php +++ b/includes/html/graphs/device/toner.inc.php @@ -6,8 +6,8 @@ $rrd_options .= ' -l 0 -E '; $iter = '1'; $rrd_options .= " COMMENT:'Toner level Cur Min Max\\n'"; -foreach (dbFetchRows('SELECT * FROM toner where device_id = ?', [$device['device_id']]) as $toner) { - $colour = toner2colour($toner['toner_descr'], 100 - $toner['toner_current']); +foreach (dbFetchRows('SELECT * FROM printer_supplies where device_id = ?', [$device['device_id']]) as $toner) { + $colour = toner2colour($toner['supply_descr'], 100 - $toner['supply_current']); if ($colour['left'] == null) { // FIXME generic colour function @@ -46,15 +46,15 @@ foreach (dbFetchRows('SELECT * FROM toner where device_id = ?', [$device['device $hostname = gethostbyid($toner['device_id']); - $descr = safedescr(substr(str_pad($toner['toner_descr'], 16), 0, 16)); - $rrd_filename = rrd_name($device['hostname'], ['toner', $toner['toner_index']]); - $toner_id = $toner['toner_id']; + $descr = safedescr(substr(str_pad($toner['supply_descr'], 16), 0, 16)); + $rrd_filename = rrd_name($device['hostname'], ['toner', $toner['supply_index']]); + $id = $toner['supply_id']; - $rrd_options .= " DEF:toner$toner_id=$rrd_filename:toner:AVERAGE"; - $rrd_options .= " LINE2:toner$toner_id#" . $colour['left'] . ":'" . $descr . "'"; - $rrd_options .= " GPRINT:toner$toner_id:LAST:'%5.0lf%%'"; - $rrd_options .= " GPRINT:toner$toner_id:MIN:'%5.0lf%%'"; - $rrd_options .= " GPRINT:toner$toner_id:MAX:%5.0lf%%\l"; + $rrd_options .= " DEF:toner$id=$rrd_filename:toner:AVERAGE"; + $rrd_options .= " LINE2:toner$id#" . $colour['left'] . ":'" . $descr . "'"; + $rrd_options .= " GPRINT:toner$id:LAST:'%5.0lf%%'"; + $rrd_options .= " GPRINT:toner$id:MIN:'%5.0lf%%'"; + $rrd_options .= " GPRINT:toner$id:MAX:%5.0lf%%\l"; $iter++; }//end foreach diff --git a/includes/html/graphs/toner/auth.inc.php b/includes/html/graphs/toner/auth.inc.php index 032e62ab20..ad3eb3026a 100644 --- a/includes/html/graphs/toner/auth.inc.php +++ b/includes/html/graphs/toner/auth.inc.php @@ -1,14 +1,14 @@ where('device_id', $device['device_id'])->get()->groupBy('toner_type'); +$supplies = \App\Models\PrinterSupply::query()->where('device_id', $device['device_id'])->get()->groupBy('supply_type'); foreach ($supplies as $type => $supply) { if (! empty($supply)) { @@ -13,25 +13,25 @@ foreach ($supplies as $type => $supply) {
' . overlib_link($link, $toner['toner_descr'], $overlib_content) . ' | +' . overlib_link($link, $toner['supply_descr'], $overlib_content) . ' | ' . overlib_link($link, $minigraph, $overlib_content) . ' | ' . overlib_link($link, print_percentage_bar(200, 20, $percent, null, 'ffffff', $background['left'], $percent . '%', 'ffffff', $background['right']), $overlib_content) . ' | diff --git a/includes/html/pages/health/toner.inc.php b/includes/html/pages/health/toner.inc.php index 148f8b2804..b93114eea7 100644 --- a/includes/html/pages/health/toner.inc.php +++ b/includes/html/pages/health/toner.inc.php @@ -34,11 +34,11 @@ $pagetitle[] = 'Health :: Toner';|||||||||
Device | -Toner | -Type | +Toner | +Type | Used | -Usage | +Usage |
---|