#!/usr/bin/env python#coding=utf-8#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||#| id  |  service  |  pid  |  time  |  status  |  runtime  |#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||#Author:dyang#该脚本实际设计用来监控windows某服务和其进程状态的。#该脚本设计了两个表,detail_table和result_table,默认5分钟取一次,把结果先写到detail_table,然后把最近两次的结果比较,如果pid发生变化或者服务stop了,#就会记录到result_table(所以看结果只看result_table就行)。#import win32serviceutil#import win32serviceimportsqlite3importosimporttime#svcType, svcState, svcControls, err, svcErr, svcCP, svcWH =  win32serviceutil.QueryServiceStatus('Dhcp')#if svcState==win32service.SERVICE_STOPPED:#print 'stopped'#elif svcState==win32service.SERVICE_RUNNING:#print 'running'#elif svcState==win32service.SERVICE_START_PENDING:#print 'starting'#elif svcState==win32service.SERVICE_STOP_PENDING:#print 'stopping'defcreatDBandTable(db,dtn,rtn):#创建tablecx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute('create table %s(id integer primary key,service varchar(30),pid integer,time TEXT(30),status integer(10),runtime integer)'%dtn)

cu.execute('create table %s(id integer primary key,service varchar(30),pid integer,time TEXT(30),status integer(10),runtime integer)'%rtn)

cx.commit()

cx.close()defgetInfo(processname):#通过tasklist命令,找到processname的pidtask=os.popen('tasklist')ifprocessnameintask.read():try:

num=os.popen('tasklist').read().split().index(processname)

tasklist=os.popen('tasklist').read().split()

index_n=num+1pid=int(tasklist[index_n])

time1=time.ctime()

status=1#status = 1 means runningexceptException,e:printeprint'Sorry ,it ecounter a exception...'status=0#status = 0 means stoppedpid=0

time1=time.ctime()else:print'Sorry,the process is not in process list,may be not running......'status=0

pid=0

time1=time.ctime()return(pid,time1,status)definsertDetail(db,detail_table,svc,pid,time1,status,rt1):#每次的结果插入到detail_table,返回表中最后一行的id号cx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute("insert into %s(id,service,pid,time,status,runtime) values(NULL,'%s',%d,'%s',%d,%d)"%(detail_table,svc,pid,time1,status,rt1))

cx.commit()

rowid=cu.lastrowid

cx.close()returnrowiddefinsertResult(db,result_table,svc,pid,time1,status,rt2):#当监控服务和进程发生变化,将结果插入到result_table,并返回表中最后一行的id号cx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute("insert into %s values(NULL,'%s',%d,'%s',%d,%d)"%(result_table,svc,pid,time1,status,rt2))

cx.commit()

rowid=cu.lastrowid

cx.close()returnrowiddefgetpid(db,rowid,table):#根据id号,得到表中的pid列的数据cx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute("select pid,runtime from '%s' where id=%d"%(table,rowid))

value,run_t=cu.fetchone()

cx.close()return(value,run_t)defgetStatus(db,rowid,table):#获得表中的status列的值,如果是0,表示stop;1表示runningcx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute("select status,runtime from '%s' where id=%d"%(table,rowid))

value,runtime1=cu.fetchone()

cx.close()return(value,runtime1)defupdateResultDB(db,table,rowid,rt3):#更新result_table。因为有一种情况,当监控的服务down了,其进程也会消失,这是status为0。等到下一次检查时,如果status仍为0,就没有必要再插入一行到表里,我直接更新runtime即可。cx=sqlite3.connect(db)

cu=cx.cursor()

cu.execute("update '%s' set runtime=%d where id=%d"%(table,rt3,rowid))

cx.commit()

cx.close()defmain(dbn,svc,pn):

detail_table_n=svc+'_detail'#detail_table的真实名字result_table_n=svc+'_result'#result_table的真实名字                                    #初始运行时间指定为0ifnotos.path.exists(dbn):

creatDBandTable(dbn,detail_table_n,result_table_n)

pid,time1,status=getInfo(pn)#获取监控进程的pid,当前时间和状态。如果服务stop了,相应进程也会不存在,所以我只监控了进程。runtime=0ifstatus:

rowid_old=insertDetail(dbn,detail_table_n,svc,pid,time1,status,0)

rowid_r=insertResult(dbn,result_table_n,svc,pid,time1,status,0)print'Now it will sleep for 5 minutes...'time.sleep(5*60)#pid_old,a = getpid(dbn,rowid_old,detail_table_n)whileTrue:

pid_old,a=getpid(dbn,rowid_old,detail_table_n)

pid,time1,status=getInfo(pn)

rowid_d=insertDetail(dbn,detail_table_n,svc,pid,time1,status,300)#强制detail_table的rt都为300,不在此记录rowid_old=rowid_d

pid_new,a=getpid(dbn,rowid_d,detail_table_n)ifstatus==0:#如果发现进程退出了status_1,runtime=getStatus(dbn,rowid_r,result_table_n)ifstatus_1:#检查detail表中上一次记录,是不是状态也为0,如果上一次是1,说明该进程是刚刚stop的,我插入一条记录到result_table;如果上一次是0,我只update result_table的runtime即可。print'---------------insert-----------------'rowid_r=insertResult(dbn,result_table_n,svc,pid,time1,status,0)

time.sleep(5*60)#rt1 += 5#rt = 0continueelse:print'status_1 is 0,just update DB...'runtime+=5updateResultDB(dbn,result_table_n,rowid_r,runtime)

time.sleep(5*60)continueelse:#如果进程未退出status_2,runtime_r=getStatus(dbn,rowid_r,result_table_n)ifstatus_2:#上次未退出ifpid_old!=pid_new:#如果pid发生了变化rowid_r=insertResult(dbn,result_table_n,svc,pid_new,time1,status,0)

pid_old=pid_new

time.sleep(5*60)else:#没有变化,则只更新runtimeruntime_r+=5updateResultDB(dbn,result_table_n,rowid_r,runtime_r)

time.sleep(5*60)continueelse:#上次退出了rowid_r=insertResult(dbn,result_table_n,svc,pid,time1,status,0)else:print'Pls. check whether the server is runing...'print'Over!'if__name__=='__main__':

db_name="c:\\EsgLS.db"#数据库文件保存的位置service_name='WebsenseEsgLogServer'#监控的服务名称pid_name='EsgLogServer.exe'#相应的进程名字main(db_name,service_name,pid_name)

python监控某个程序_9-30 python监控windows某个进程的变化(修正版)相关推荐

  1. 30岁自学python找工作-程序员自学Python开发,20到30岁几乎决定了你的未来!

    原标题:程序员自学Python开发,20到30岁几乎决定了你的未来! 之前程序员界流行一句话:人生苦短,请用Python. 随着Python成为网红语言之后,不少程序员想多学这一门语言好傍身. 甚至有 ...

  2. python开发安卓程序-如何使用python开发android应用

    Python是一种面向对象.解释型计算机程序设计语言,其源代码和解释器CPython遵循GPL(GNU General Public License)协议Python语法简洁清晰,特色之一是强制用空白 ...

  3. python开发安卓程序-如何使用python开发Android手机应用?

    在使用python开发android应用之前我们需要准备好环境,环境需要安装PythonForAndroid,然后开始编程,编程时打开eclipse, 创建一个新的android project &q ...

  4. python写机器人程序_用Python写的一个多线程机器人聊天程序

    本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...

  5. java min 函数的使用方法_【Python】Java程序员学习Python(五)— 函数的定义和使用...

    不想做一个待宰的羔羊!!!!要自己变得强大.... 函数的定义和使用放在最前边还是有原因的,现在语言趋于通用,基本类型基本都是那些,重点还是学习对象的使用方法,而最根本的还是方法的使用,因此优先介绍, ...

  6. python二分法查找程序_查找Python程序的输出| 套装2(基础)

    python二分法查找程序 Program 1: 程序1: a = 10 b = 3 res = a/b print "a/b: ", res res = float(a/b) p ...

  7. 为什么一个程序中变量只能定义一次_#带你学Python# 从简单程序出发理解Python基本语法

    欢迎回来. 通过上一篇文章,我们第一次触摸了Python,学会了如何用各种不同的方式运行Python的解释器.也介绍了很多工具和开发环境,是不是跃跃欲试了? 到这里,别的python教程就会从数据类型 ...

  8. 用python写投票程序_大话python最终篇,web.py 开发的投票程序demo

    概述 开发语言         python Web开发框架  web.py 前端开发框架   vuejs+elementui 数据库              mysql 设计思路 首先是数据库设计 ...

  9. python 监控股价 程序 tk_linux通过python监控股票股价

    python 脚本代码如下,保存文件名为 gupiao.py: #!/usr/bin/env python # -*- coding:utf-8 -*- from colorama import in ...

最新文章

  1. layoutSubviews 调用情况
  2. Batch Normalization学习笔记
  3. 最高科技——疯狂的前缀和
  4. 并联匹配和串联匹配的原理和选择
  5. Java基础之HashMap流程分析
  6. LateUpdate、Late、FixedUpdate的意义
  7. lib 和 dll 的区别、生成以及使用详解
  8. ElasticSearch安装过程中遇到的一些问题
  9. 9008刷机怎么刷_手机刷机怎么刷
  10. c++ 终止 超时_c++超时问题
  11. 为什么说堡垒机是企业IT运维的“安全终结者”?
  12. js excel 矫正
  13. 由于计算机是中文名ccs软件安装出现错误_UG软件不会解决的二十个问题解决方法总结...
  14. Python模拟微信发红包
  15. Android中的EditText输入银行卡号四位空一格
  16. 1 入门:投身新领域
  17. Spring学习笔记(上)
  18. c语言程序设计五子棋棋盘怎么画,五子棋的棋盘画不出来,求助!!
  19. 物联网卡充值续费仍无法使用,关键原因在这里!
  20. JS 数组动态添加键值对

热门文章

  1. JAVA8两个流变量相等_Java8:Stream在同一个流中映射两个属性
  2. 【Linux系列】mac终端与服务器连接
  3. mysql dns反向解析_DNS练习之反向解析(示例代码)
  4. PHP中Cookie的使用
  5. js中 switch 注意事项
  6. pythopn 函数(内置函数)
  7. Mybatis系列(四):Mybatis缓存
  8. ubuntu之Matlab安装
  9. 《Arduino家居安全系统构建实战》——1.7 小结
  10. Angular 学习笔记——拖拽