监控Java cpu、内存自动抓取 jstack 日志脚本-升级版

监控Java cpu、内存自动抓取 jstack 日志脚本-升级版

环境

python 2.7

背景

旧版

之前写过 监控Java cpu、内存自动抓取 jstack 日志脚本
。但是有点简单只能查看一个端口进程,发现满足不了需求现对其进行改版。

新版

支持多个端口进程,支持发送短信通知(如不需要则注释)。

源码

view_cup_mem.py

# -*- coding: utf-8 -*-
import time
import commands as cm
import os
import sys
import urllib
import urllib2

global_date = ''
# 发送短信方法, 一天只发送一次,避免影响
def senMsg(sendTxt):
    global global_date
    time_str = time.strftime("%Y%m%d", time.localtime())
    if(time_str != global_date):

        data = {}
        url = 'http://localhost:9191/sms/phoneMsg/msmpush'  # 修改自己的地址 <<------------------!!!!!!!!!
        data['mobilelist'] = '12322223333'  # 修改自己的手机号码   <<------------------!!!!!!!!!
        data['content'] = sendTxt
        params = urllib.urlencode(data)
        response = urllib2.urlopen('?'.join([url, '%s']) % params)
        print 'response: %s' % response.read()
        global_date = time_str
    else:
        print 'today send ------- '  # 今日已经发过一次
# 执行方法
def executeFun(port):
    # port = '8090'
    # 获取进程命令 pid_comm ;
    # netstat -ntpl|grep port  : 获取端口号port 的进程信息
    # awk '{printf $7}' : 取第七列
    # cut -d/ -f1  : 以 / 分割后取第一个值
    pid_comm = 'netstat -ntpl|grep ' + port + '|awk \'{printf $7}\'|cut -d/ -f1'
    # 取得进程 pid
    pid = cm.getoutput(pid_comm)
    if (pid == ''):
        print 'no pid'
        return
    # 获取cup, mem百分比命令
    # top -p  pid  -n1 : 获取进程 pid 的top 信息,只刷新一次。
    # sed -n '8p'   : 获取第8行
    # awk '{printf $9}' : 获取第9列

    cup_comm = 'top -b -p  ' + pid + '  -n1|sed -n \'8p\'|awk \'{printf $9}\''
    # 内存
    mem_comm = 'top -b -p  ' + pid + '  -n1|sed -n \'8p\'|awk \'{printf $10}\''

    # 取得cpu 占用百分比
    cpu_perc_str = cm.getoutput(cup_comm)
    # 取得内存 占用百分比
    mem_perc_str = cm.getoutput(mem_comm)

    # 转成数字类型
    cpu_perc = float(cpu_perc_str)
    mem_perc = float(mem_perc_str)

    print cpu_perc
    print mem_perc
    # 获取时间串
    time_str = time.strftime("%Y%m%d%H%M%S", time.localtime())
    print time_str
    log_dir = '/hnpwqx/gclogs/app_server/'  # 日志存放目录, 自定义!!!         <<------------------!!!!!!!!!

    pid_exists = len(cm.getoutput('jps |grep ' + pid)) > 0           # pid_exists 是否存在java 进程 id

    if (cpu_perc > 1000.1):  # 百分比大于多少执行, 自定义!!!                                      <<------------------!!!!!!!!!

        if(pid_exists):
            jstack_comm = 'jstack -l ' + pid + ' >> ' + log_dir + 'gc_' + time_str + 'port_'+port+'_cpu_' + cpu_perc_str + '.log'  # 保存 jstack 日志
        else:
            text = 'port_ '+port +'_cpu_' + cpu_perc_str+'time_'+ time_str
            jstack_comm = 'echo '+text+' >> ' + log_dir +'other_cup.txt'
        os.system(jstack_comm)
        senMsg(jstack_comm)  # 发送短信        不需要则注释  <<------------------!!!!!!!!!
    elif (mem_perc > 40.1):
        if (pid_exists):
            jstack_comm = 'jstack -l ' + pid + ' >> ' + log_dir + 'gc_' + time_str + 'port_'+port+'_mem_' + mem_perc_str + '.log'   # 保存 jstack 日志
        else:
            text = 'port_ ' + port + '_mem_' + mem_perc_str + 'time_' + time_str
            jstack_comm = 'echo ' + text + ' >> ' + log_dir + 'other_mem.txt'
        os.system(jstack_comm)
        senMsg(jstack_comm)  # 发送短信    不需要则注释  <<------------------!!!!!!!!!
    # 删除7天前的 log 结尾文件
    rm_log_comm = 'find ' + log_dir + ' -mtime +7 -type f -name "*.log" -exec rm -f {} \;'
    os.system(rm_log_comm)
if __name__ == '__main__':
    ports = sys.argv[1:]
    while (True):
        for port in ports:
            executeFun(port)
        # 延迟30, 自定义!!!                                             <<------------------!!!!!!!!!
        time.sleep(30)

执行如下

// nohup  后台执行
nohup  python -u  view_cup_mem.py  8080 8081 8082  &
# $language = "VBScript" # $interface = "1.0" '作者;小怪兽怪瘦 Sub Main Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso,file1,line,str1,params Set fso = CreateObject("Scripting.FileSystemObject") Set file1 = fso.OpenTextFile("D:\backup\list.txt",Forreading, False) crt.window.show 3 crt.Screen.Synchronous = True do while file1.AtEndOfStream True '读取每一行 line = file1.ReadLine '分割IP,用户名,密码 特权密码 型号简写(z m h b) 位置 params = Split (line) ip = params(0) user = params(1) password = params(2) su = params(3) mode = params(4) whereis = params(5) crt.session.LogFileName = "D:\backup\log\"&ip&"_"&whereis&"_20190620.txt" '提供日志,修改后面日期即可 crt.session.Log(true) crt.Session.Connect ( "/SSH2 /L "&user&" /PASSWORD "&password&" "&ip) '建立SSH连接 if mode = "m" then 'm 代表迈普型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "more off" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-mp.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "more on" & chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "exit" & chr(13) crt.Screen.WaitForString ">" crt.Session.Disconnect end if if mode = "c" then 'c 代表思科型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-cisco.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "r" then 'r 代表锐捷型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-rj.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "z" then 'z 代表中兴型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-zx.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) if (crt.Screen.WaitForString (" --More--",1)False) then crt.Screen.Send " " & chr(13) else crt.Screen.Send chr(13) end if crt.Screen.waitForString "#" loop 'crt.Screen.Send "show running-config" & chr(13) 'crt.Screen.WaitForString " --More--" 'crt.Screen.WaitForString "#" crt.Session.Disconnect
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值