1.proc.py

#/usr/bin/python

import subprocess

import os

import socket

import json

import glob

import time

import datetime

javas=[]

java_dir_proc='xx'

now = datetime.datetime.now()

strdatetime = now.strftime("%Y/%m/%d")

java_dir_home=['ip1','ip2']

def get_exception(java_dir):

a = os.listdir(java_dir)

for x in a:

#javapath = os.path.join(java_dir, x)

#if os.path.isdir(javapath):

   if len(x) != 0:

   javas.append({'{#EXCEPTION}':x.strip('\n')})

for i in java_dir_home:

    java_dir=java_dir_proc + i + '/' + strdatetime

    get_exception(java_dir)

news_javas = []

for id in javas:

    if id not in news_javas:

        news_javas.append(id)

print json.dumps({'data':news_javas},indent=4,separators=(',',':'))

2.proc_num.py

#!/usr/bin/python

import sys

import subprocess

import os

import socket

import json

import glob

import time

import datetime

zdict={}

zd=[]

javas=[]

java_dir_proc='xx'

now = datetime.datetime.now()

strdatetime = now.strftime("%Y/%m/%d")

java_dir_home=['ip1','ip2']

def get_exception(javapath):

        if os.path.isdir(javapath):

            #CMD="cd javapath && ls|wc -l"

            os.chdir(javapath)

            CMD='ls|wc -l'

            t=subprocess.Popen(CMD,shell=True,stdout=subprocess.PIPE,env=None)

            for java in t.stdout.readlines():

                if len(java) != 0:

                        S=java.strip('\n').strip(':')

                        return S

if len(sys.argv) == 2:

    argv1 = sys.argv[1]

    for i in java_dir_home:

    javapath=java_dir_proc + i + '/' + strdatetime + '/' + argv1

        zdict[i]=get_exception(javapath)

        #print zdict[i]

        zd.append(int(zdict[i]))

    print reduce(lambda x,y:x+y,zd)

else:

    print -1

3.cat check_proc_exception.conf 

UserParameter=proc.discovery,/usr/bin/python /usr/local/zabbix/bin/proc.py

UserParameter=proc.num[*],/usr/bin/python /usr/local/zabbix/bin/procnum.py $1

4.建立模板

5.其实还可以写的更好

比如

UserParameter=proc.num[*],/usr/bin/python /usr/local/zabbix/bin/procnum.py $1 $2

对应于proc.num[{#EXCEPTION},$2]

写的差,有几个地方要优化下。比如+,怎么连接字符串。

6.图像



改进

#/usr/bin/python
import subprocess
import os
import socket
import json
import glob
import time
import datetime
javas=[]
java_dir='xx'
now = datetime.datetime.now()
strdatetime = now.strftime("%Y/%m/%d")

def get_exception(java_dir):
	a = os.listdir(java_dir)
	for x in a:
            if x == "nginx" or x == "api" or x.startswith('.'):
                 pass
            else:
                 javas.append({'{#EXCEPTION}': x.strip('\n')})


if __name__ == '__main__':
    get_exception(java_dir)
    news_javas = []
    for id in javas:
        if id not in news_javas:
            news_javas.append(id)
    print json.dumps({'data': news_javas}, indent=4, separators=(',', ':'))
#!/usr/bin/python
import sys
import subprocess
import os
import socket
import json
import glob
import time
import datetime
zdict={}
zd=[]
javas=[]
java_dir='xx'
now = datetime.datetime.now()
strdatetime = now.strftime("%Y/%m/%d")

def get_exception(javapath):
        if os.path.isdir(javapath):
            os.chdir(javapath)
            CMD='find . -type f|wc -l'
            t=subprocess.Popen(CMD,shell=True,stdout=subprocess.PIPE,env=None)
            try:
                for java in t.stdout.readlines():
                    if len(java) != 0:
                        S = java.strip('\n').strip(':')
                        return S
            except e:
                return 0



if len(sys.argv) == 3:
    argv1 = sys.argv[1]
    argv2 = sys.argv[2]
    file = '/usr/local/zabbix/scripts/tomcat.txt'
    with open(file, 'r') as f:
        for line in f.readlines():
            name = line.strip().split()[0]
            if argv1 == name:
                ip = line.strip().split()[1]
                log_path = os.path.join(java_dir,argv1,ip,argv2,strdatetime)
                a_num = get_exception(log_path)
                if a_num:
                    zd.append(int(a_num))
                else:
                    zd.append(0)
        print reduce(lambda x, y: x + y, zd)
else:
    print -1