zabbix monitor printer and automatically updates the monitored items

No monitoring is not operation and maintenance, this is wisdom;

zabbix powerful self-evident here, teach you to use it to solve a very common requirement today, operation and maintenance of the liberation of hands, inspired by everyone's ideas, if you put your heart to see the end, we must gain something:

Briefly introduce requirements:

Now many companies are leasing printers, because to buy than rent, per month scanner to see the use of consumables, inform lessors timely replacement of consumables, the boss needs to know the monthly print volume, and so on;

If a multifunction printer is better to say, that if many units it? Have you ever had the following dilemma:

Monthly need to manually go in front of the printer to print paper statements or web log in to view the printer spool print run and consumables usage;

We need to send supplies to the scanner usage and service providers a single monthly bill;

Monthly usage statistics need to manually make a report to the boss inspection;

Supplies not replaced, leading to delays in work led off for personnel departments yelling, complaining about the complaint;

Since the signing of the contract and the supplier is rated print run is 9K / month, exceeding this number for an additional fee, many cases are not finished using the normal waste but finished,

If the number of copies reaching 80% of the rated operation and maintenance will be automatically prompted to print a timely review of the abnormal behavior of (or boss issued directly to this demand), the value of your existence on the highlights out;


So zabbix can help you:

But even deal with the use of zabbix also do some tricky:

For example, by direct acquisition snmp printer print run is shipped to the current accumulated value, and operation and maintenance and the boss needs to see the actual monthly amount (you can not expect the boss holding a calculator to two months scanner subtraction),

So after each scanner is finished, the scanner needs to be cleared;

The printer is a service provider, you can not go to clear each month, then only the hands and feet in zabbix above, use the formula to lose the current scanner to achieve the purpose of clearing, we are also doing a month a pain;

So be patient to see the back will be introduced with a script to automate.


The first step: Turn the printer snmp service, if no snmp business equipment now, I can only say that it did not want to mix in the IT sector, and small home router, Cisco millions of large equipment have this:

With our company Samsung K3250NR printer for instance:

Pictures .png

Of course, it can also took the trouble to open snmpv3;


Login zabbix create a monitoring host:

Pictures .png

通过查询打印机官方的SNMP MIB文档或者使用snmpwalk分析OID监控项(玩SNMP要是不会这些,下面也不用看了),找到需要的监控项键值:

监控墨粉耗材使用量:

Pictures .png

监控当前累计印数(这个通常不能清零,否则供应商还吃什么)

Pictures .png

查看数据采集情况:

图片.png

这样打印机的一举一动就被监控起来了,但是手工活一点没少,我这人很懒,我连zabbix都不想去看,供应商我也不想去找,电脑能够自动完成的事干嘛要人去惦记?

我只想,每月头,它自动发送抄数和耗材使用情况给供应商并CC一份给我就好,遇到耗材低的时候自己出邮件通知供应商来上门更换。

哦,对了,三星高级打印机有自动定时发送报表邮件功能,但是,呵呵,这个安卓固件实在太烂,测试时候是成功的,过不了多久就罢工,真心没法儿指望;

对的,Linux的任务计划可以做到,每月一号自动抄数,耗材还好说,印数是个累计值,抄数完毕还要从当前抄数自动从零计数并反映到zabbix里面,这个就需要一点技巧;

这里使用zabbix 提供的API在抄数的同时,将当前累计抄数减掉并更新监控项,不要我每个月去手工改监控项:

对的,就是实现下面这个监控项的公式自动更新(我也是懒得可以):

图片.png

上点干货,在linux下面创建一个python脚本,并加入crontab里面去定时运行:

# !/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'  #根据自己的情况修改下参数
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  #这里调用外部shell脚本更新zabbix监控项
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'>打印机每月抄数("+last_month+"月)</th></tr>"
head = "<tr bgcolor='3F48CC'><th><font color='ffffff'>三星K3250NR打印机</font></th><th><font color='ffffff'>本 次 抄 数</font></th></tr>"
sent_content ="<table bgcolor='E2FFC5' border='1' align='center' cellspacing='5'><tr><td>" +  title + head +"<tr><td>打印机印数</td><td>" +print_count + '</td></tr><tr><td>墨粉仓剩余</td><td>'+ SupplyUnit +'%</td></tr></table></td></tr></table>'

mail_host = "你的邮箱服务地址"
mail_user = "发送的邮箱账号"
mail_pass = "邮箱密码"

sender = '发送的邮箱账号'
receivers = ['自己的邮箱和供应商邮箱群组']

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



subject = '打印机每月抄数('+last_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")

贴上

item_update.sh
token=$(./zabbix_api.sh) #这里又调用外部脚本zabbix_api.sh去获取API的token ,下面如果连itemid都不知道怎么改也就不用玩zabbix了
ZBX='zabbix服务器的IP地址'
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

贴上

zabbix_api.sh
#!/bin/bash
admin=Admin #zabbix用户
pass=zabbix #密码
ZBX='x.x.x.x' #zabbix-server的ip地址
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 that the message is received:

图片.png

zabbix monitoring items are automatically updated count:

图片.png

Which can be seen in grafana count is cleared traces of:

图片.png

There may also be interested in the use of low supplies and alarm messages immediately notify the supplier, this is not a piece of cake for zabbix?

图片.png


Guess you like

Origin blog.51cto.com/kingda/2425706