Zabbix. What if the printer is not sharing information via SNMP?

Good day!



imageKDPV



This article is a small manual written in a somewhat artistic form. There are many pictures in the article, carefully hidden in spoilers.



Who is this article for? For novice sysadmins who are still little familiar with UNIX systems, Zabbix, but who want to centrally collect information from printers.



Introductory. Available:



  • Zabbix 5.0
  • CentOS 8
  • A printer that does not send the necessary information about consumables via SNMP (in this manual, Oki C834 is taken as an example)
  • Virtually absent knowledge of Zabbix, UNIX systems
  • There is a great desire to collect information about consumables from printers centrally.


Go!



Reasons and rationales



Laziness is the engine of progress. One fine morning, I realized that I was too lazy to go around the printers installed in the office in order to monitor the status of consumables such as toner cartridges, drum units, ribbons and ovens. And also collect from time to time the indicators of the counters of the printed pages - on their basis, a decision is made about the maintenance of the printer.



"Well this is how much time you can save by collecting such information centrally!" - this thought did not leave my head. It was decided to deploy a monitoring system. Zabbix was chosen with a hint from older comrades - with the expectation that in addition to printers, monitoring of servers and network equipment would gradually be connected.



CentOS 8 was installed as the operating system - for I was at least a little familiar with it. Perhaps someone is already writing at the moment that ay-ay-ay, you need to study UNIX systems, if you have already taken up system administration. My answer is yes, you are absolutely right.



Studying the manuals for monitoring printers promised an extremely simple procedure - snmpwalk, isolating the necessary parameters, connecting the OID to Zabbix, profit. Reality has shaken this beautiful picture a little. There were no indicators of the condition of consumables. There weren't even remotely similar ones. Generally.



Article Getting statistics from Kyocera devices in Zabbixprompted a search for proprietary software for monitoring the printer and intercepting communication between the software and the printer. It was found - Configuration Tool, a utility from Oki for monitoring the status of printers. Download, run, and…. via SNMP Configuration Tool receives information about the printer name, MAC address, and software version. Information about consumables is transmitted in the body of TCP / IP packets.



“Well, great, the adventure begins,” I thought. Thinking about solution options led to the following logical chain:



  1. At this stage, I do not know how and do not know how to correctly simulate the request that the Configuration Tool sends to the printer.
  2. The printer has its own web page where it provides information about the status of supplies and the number of pages printed
  3. The solution to my problem is to extract (parse) information from the printer's web page on a schedule and somehow transfer this information to Zabbix.


Searching for a parsing tool gave the following results:



  1. Bash
  2. PHP
  3. Python


Note: I am quite aware that there are more parsing tools than are shown in this list - but it makes no sense to indicate all of them, and then you will understand why.



I chose Bash - and here's why:



  1. Pre-installed on most UNIX systems. I don't need to install additional software.
  2. Simplicity. The commands are logical, and quite understandable after a little study of the syntax.
  3. The need to learn UNIX systems. Obviously, it will not be superfluous to study the command syntax in this direction.


Parse a printer web page



Before we start parsing, let's take a look at what a printer web page is.



Note
, - . , , , -.



-



Printer web page
image



Notice that there are toner status indicators on the top right of the page. Now it remains for us to understand how these indicators appear on the web page. RMB-> Inspect. The following picture opens to us.



Page code inspection
image

image



Note that the toner values ​​are actually obtained through the /status.htm page.

We see that the toner values ​​are passed in tags:



After that, using JavaScript, the values ​​are rounded to tens and inserted into the page.



Go to page 192.168. ***. *** / status.htm - and yes, we see the toner values ​​we really need on this page



status.htm
image



We now know where the toner status values ​​we want are stored. Let's start parsing.



Parsing



Note: the following will be a description of building a script for parsing a page with examples and pictures. I will try to make the description clear even for those who are very little familiar with Bash. If you want to read the result already, please scroll further.

So Bash. The Rubicon has been passed, as they said two thousand years ago, on the banks of the river of the same name.

First of all, I suggest that you familiarize yourself with the syntax of the language . It is extremely simple and straightforward - but it will help us in scripting and automation.



Next, let's pay attention to cURL - command line and tool, as stated on the official websitethis tool. cURL supports a huge number of protocols, of which we need support for HTTP - remember that this is the protocol used to "communicate" with the printer's web page.

Let's write the first request:



$ curl 192.168.***.***/status.htm


Terminal output
image



In response, a huge amount of information is dumped directly into the command line, which is not very convenient to read in the terminal, not to mention the processing of this information.

Let's redirect the output to a file - this will obviously be more convenient. The special character '>' will help us with this.



$ curl 192.168.***.***/status.htm > Oki.txt


Open the resulting file in the Vi editor



$ vi Oki.txt


Oki.txt in the Vi editor
image



Much better, isn't it? However, we don't need the entire page anyway. Only the toner status values ​​are needed. We begin to isolate the data. This is where grep, a utility that searches for lines containing a value equal to a specified regular value, will help us.



Take a close look at the tags that contain the toner values. We see that in them we meet the same expression 'value = ”***”'



Note
? - 100.



Note
, “hidden”. , . . – , ‘value=”***”’



image



We "rip" the results obtained through cURL. In order to speed up and visualize the process a little, we output the values ​​directly to the terminal.



$ curl 192.168.***.***/status.htm | grep ‘value=”***”’ 


Grep results
image



Excellent. Concise enough conclusion. Next, we need to get rid of the text and leave only the numerical values.



Note
, – « Zabbix’ !». , . – . .



We filter out text data, leaving only numeric values. Grep with the -Eo flag will help us with this again. It will allow only numeric values ​​to be displayed.



$ curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’


Outputting numbers
image



So, we got the required numerical values. Now let's analyze our output into separate lines so that we can save them separately to text files. This is where sed, a stream text editor utility, comes in handy. In our case, the most important thing is that sed can isolate individual lines - which is exactly what we need.



Curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’ | sed -n 1p


Sed output
image



Let's take a look at the sed part. 1p - output the first line. -n - exclude everything except the specified line. As you can see, after this command, the output shows only the numerical value of the cyan toner status.



Now let's start writing a script.



Bash scripts have a certain peculiarity compared to the same bat scripts. They must begin with a shebang - "#!". Further lines starting with # will be interpreted as comments.



The script will look like this:



#! /bin/bash 
#This script received toner level's from Oki_834
curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’ | sed -n 1p >/var/cyan.txt
curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’ | sed -n 2p >/var/magenta.txt
curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’ | sed -n 3p >/var/yellow.txt
curl 192.168.***.***/status.htm | grep ‘value=”***”’ | grep -Eo ‘[0-9]{1,}’ | sed -n 4p >/var/black.txt


Add the script file to crontab, and thereby automate its execution. The author has set * / 15 * * * * - execution every 15 minutes.



Bottom line - the printer's web page is automatically parsed, we get the numerical values ​​we need and save them in the directory we need.



Transferring received values ​​to Zabbix



Now let's get down to transferring data to Zabbix.



Note
, , . , , .



Let's create a new item. We name it whatever we like, we specify the type Zabbix agent.



ATTENTION
Zabbix-server Zabbix-agent. , , Zabbix-agent.



The most interesting and important thing we have at this stage is the key. We need vfs.file.contents [file, <encoding>]. File is the absolute path to the file.



Create item in Zabbix
image



We indicate our path to the file and check the correctness. On the test output, we should see our black toner value.



Successfully completed test
image



Bottom line - we have migrated data to Zabbix, and now we can control the status of consumables centrally.



Conclusion



We have mastered obtaining data by parsing web pages. In this example, the Oki 834 printer was taken and the toner data acquisition was successful. I responsibly declare that the same method was used to obtain data on the state of drum units, tape and oven.



Screenshot evidence
image



image



Thus, it is possible to obtain information about consumables from almost all printers, and not only about printers. Parsing is a fairly convenient tool, and I really liked working with it. And if my article helps at least one person, then it was not in vain.



I also want to express my deep gratituderedheaddeer, akozhevnikov97and a person who wished to be anonymous for their help in writing the article. Thanks to all!



All Articles