zabbix monitors printers and automatically updates monitoring items

Keywords: Linux Zabbix shell curl

It is a wise saying that there is no monitoring and no maintenance.

The strength of zabbix is self-evident here. Today, we teach you how to use it to solve a common need, liberate the hands of operation and maintenance, and inspire your thinking. If you see the end with your heart, you will get something:

First, the requirements are briefly introduced.

Nowadays, printers in many enterprises are leased, because it is better to buy than rent. Every month, we need to copy the number, check the usage of consumables, notify the renter to change the consumables in time, the owner needs to know the monthly printing amount, and so on.

If a multi-function printer is good, what if there are many? Have you ever had the following embarrassment?

Every month, you need to manually print paper reports in front of the printer or log on to the printer's background on the web to check the print number and consumables usage.

Need to send consumables usage and transcription to service providers monthly bills;

Monthly manual statistics are needed to make reports for the employer to check.

Consumables were not replaced in time, resulting in delays in the work of supply interruption, resulting in department users shouting and complaining.

Because the contract signed with the supplier is rated at 9K per month, there is an additional charge for exceeding this number. In many cases, it is not used up normally, but wasted.

If you automatically prompt Operations and Maintenance to review abnormal printing behavior in time when you reach 80% of the rated printing number (or if your boss directly sends this demand), your value will be highlighted.


So zabbix can help you:

But even using zabbix requires some skillful handling:

For example, the print directly collected through snmp is the cumulative value of the printer from the factory to the present, while the peacekeeping boss needs to see the actual monthly usage (you can't expect the boss to take a calculator to subtract the two-month transcription).

Therefore, after each transcription, it is necessary to zero the transcription.

The printer is a service provider, it is impossible to clear the zero for you every month, so you can only do something on zabbix, using the calculation formula to reduce the current transcription to achieve the goal of zero, and doing so every month is also a pain.

So be patient to see that the script will be introduced later to automate the completion.


Step 1: Turn on the printer's snmp service. If there is no snmp in enterprise equipment now, I can only say that it doesn't want to be mixed up in the IT industry, as small as home routers and as large as millions of thinking devices.

Take our company's Samsung K3250NR printer for example:

Of course, it's okay to bother opening snmpv3.


Log in to zabbix to create a monitoring host:

By querying the printer's official SNMP MIB documents or using snmpwalk to analyze OID monitoring items (if you don't play SNMP, you don't have to look at them below), you can find the required key values for monitoring items:

Monitor the use of toner consumables:

Monitor the current cumulative print (this is usually not zero, otherwise what the supplier eats)

View data acquisition:


So the printer's every move is monitored, but there's no lack of manual work. I'm lazy. I don't even want to see zabbix. I don't want to look for suppliers. Why do people care about what computers can automatically accomplish?

I just want to, at the beginning of each month, it automatically sends the transcription and consumables usage to the supplier and CC one to me. When the consumables are low, it sends the mail to the supplier to come and replace it.

Oh, by the way, Samsung Advanced Printer has the function of sending reports and emails automatically and regularly, but, oh, this Android firmware is so bad that it was successful in the test. It won't take long to strike, and I can't really count on it.

Yes, the task plan of Linux can be accomplished. On the 1st of each month, the number of copies can be automatically counted. The consumables are good to say, the number of copies is a cumulative value. When the copies are finished, the number of copies must be automatically counted from zero to zabbix, which requires a little skill.

Here we use the API provided by zabbix to subtract the current accumulated transcription and update the monitoring items. I don't need to change the monitoring items manually every month.

Yes, the formula of the following monitoring item is automatically updated (I'm too lazy to do that either):

To get dry, create a python script under linux and add it to crontab to run regularly:

# !/usr/bin/python3

import smtplib,time
from email.mime.text import MIMEText
from email.header import Header
import subprocess

bill_month=time.strftime('%b', time.localtime())
check_time=time.strftime("%Y-%m-%d %H-%M-%S", time.localtime())

last_month = time.localtime()[1]-1
date = time.strptime(str(last_month),'%m')
last_month=time.strftime('%m',date)

def run_cmd(cmd):
    result_str=''
    process = subprocess.Popen(cmd, shell=True,
              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result_f = process.stdout
    error_f = process.stderr
    errors = error_f.read()
    if errors: pass
    result_str = result_f.read().strip()
    if result_f:
       result_f.close()
    if error_f:
       error_f.close()
    return result_str

cmd = 'snmpwalk -v 1 -c public 192.168.130.25 .1.3.6.1.2.1.43.10.2.1.4.1.1'  #Modify the parameters according to your own situation
result = str(run_cmd(cmd))
print_count = ((result.split('Counter32: '))[1]).rstrip("'")
print(print_count)

cmd2 = 'snmpwalk -v 1 -c public 192.168.130.25  .1.3.6.1.4.1.236.11.5.1.1.3.22.0'
result2 = str(run_cmd(cmd2))
SupplyUnit = ((result2.split('INTEGER: '))[1]).rstrip("'")
#print(SupplyUnit)

cmd3 ='/root/K3250NR/item_update.sh ' +  print_count  #This calls the external shell script to update the zabbix monitor
result3 = str(run_cmd(cmd3))
#print(result3)

title = "<table border='0' cellspacing='20' align='center' style='font-size:16px;word-break: keep-all'><tr><th colspan='2'><font face='verdana' color='green'>Printer Reading Monthly("+last_month+"month)</th></tr>"
head = "<tr bgcolor='3F48CC'><th><font color='ffffff'>Samsung K3250NR Printer</font></th><th><font color='ffffff'>book second copy number</font></th></tr>"
sent_content ="<table bgcolor='E2FFC5' border='1' align='center' cellspacing='5'><tr><td>" +  title + head +"<tr><td>Print number of printer</td><td>" +print_count + '</td></tr><tr><td>Residual toner bin</td><td>'+ SupplyUnit +'%</td></tr></table></td></tr></table>'

mail_host = "Your email service address"
mail_user = "Mailbox account sent"
mail_pass = "Password"

sender = 'Mailbox account sent'
receivers = ['My own mailbox and supplier mailbox group']

message = MIMEText(sent_content, 'html', 'utf-8')
message['From'] = Header("Printer Admin", 'utf-8')
message['To'] = Header("All IT Colleagues", 'utf-8')



subject = 'Printer Reading Monthly('+last_month+'month)'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("sent success")
except smtplib.SMTPException:
    print("Error: sent faild")

Affix

item_update.sh
token=$(./zabbix_api.sh) #Here we call the external script zabbix_api.sh to get the token of the API. Next, if you don't know how to change itemid, you don't need to play zabbix.
ZBX='zabbix Server IP address'
params="last(\"K3250NR:prtMarkerLifeCount\")-'$1'"
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "item.update",
    "params": {
        "itemid": "39311",
        "params": "last(\"K3250NR:prtMarkerLifeCount\")-'$1'"
    },
    "id": 2,
    "auth": "'$token'"
}' http://$ZBX/api_jsonrpc.php

Affix

zabbix_api.sh
#!/bin/bash
admin=Admin #zabbix users
pass=zabbix #Password
ZBX='x.x.x.x' #ip address of zabbix-server
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "'$admin'",
"password": "'$pass'"
},
"id": 1,
"auth": null
}' http://$ZBX/api_jsonrpc.php|grep -Po 'result[" :]+\K[^"]+'

The final effect is to receive mail:

The zabbix monitor also automatically updates the count:

In grafana you can see that the counts are cleared:

Interested can also do a low consumption of consumables automatic alarm and e-mail to inform the supplier immediately, this is not a piece of cake for zabbix?


Posted by dragon_sa on Thu, 01 Aug 2019 03:23:30 -0700