[Python Study Notes]进程信息(丁丁软件监控进程,http-post)
生活随笔
收集整理的這篇文章主要介紹了
[Python Study Notes]进程信息(丁丁软件监控进程,http-post)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>文件: 進(jìn)程信息.py
>>作者: liu yang
>>郵箱: liuyang0001@outlook.com'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''#!/usr/bin/env python
# -*- coding: utf-8 -*-import sys,os
import psutil# http get/post
import requests
# 處理字符串
import json
# 獲取系統(tǒng)時(shí)間
import time# 丁丁url
url = 'https://oapi.dingtalk.com/robot/send?access_token=3d94c740a27d5f70da6acf2a1c9383b338ad5c562fbaece8d38595c405e7e6a9'
# 轉(zhuǎn)換成當(dāng)前的時(shí)間戳
time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# 要監(jiān)控的進(jìn)程
monitor_name = {'ufw'}
# 要監(jiān)控的進(jìn)程,與shell語(yǔ)句對(duì)應(yīng),先定義為空
monitor_map={}
#要監(jiān)控的服務(wù)名集合,{'ufw': 'service ufw start'}
for m in monitor_name:monitor_map[m]='service '+m+' start'print(monitor_map)# print(time_now)# 顯示進(jìn)程,分別得到進(jìn)程字典{'pid':'name'}和去重元祖()
def process_message():# 進(jìn)程字典,保存pid,進(jìn)程名的鍵值對(duì)proc_dict={}# 進(jìn)程列表,保存去重的進(jìn)程名稱(chēng)proc_list=set()# psutil.Process(pid=4, name='System', started='2018-02-11 16:39:45')for p in psutil.process_iter(attrs=['pid','name']):proc_dict[p.info['pid']]=p.info['name']proc_list.add(p.info['name'])print(proc_list)print(proc_dict)proc_stop = monitor_name - proc_list#proc_stop:提取出來(lái)哪一個(gè)進(jìn)程沒(méi)有在當(dāng)前操作系統(tǒng)下開(kāi)啟if proc_stop:#當(dāng)前沒(méi)有啟動(dòng)的這個(gè)進(jìn)程集合不為空,有服務(wù)死掉了for p in proc_stop:print("正在重新啟動(dòng)%s"%p)#取出每一個(gè)沒(méi)有啟動(dòng)的服務(wù)名p_status = '停止'p_name = pdata = {"msgtype": "markdown","markdown": {"title":"監(jiān)控訊息","text": "#### %s\n" % time_now +"> ##### 服務(wù)名: %s \n\n" % p_name +"> ##### 狀態(tài): %s \n" % p_status +"> ##### 正在嘗試啟動(dòng)"}}headers = {'Content-Type':'application/json ; charset=UTF-8'}send_data = json.dumps(data).encode('utf-8')requests.post(url=url,data=send_data,headers=headers)#post提交數(shù)據(jù):提交的地址,提交的數(shù)據(jù)if sys.platform.lower()=='linux':os.system(monitor_map[p])#函數(shù)不會(huì)給你返回值proc_name = set()for p2 in psutil.process_iter(attrs=['pid','name']):proc_name.add(p2.info['name'])#操作系統(tǒng)下的所有進(jìn)程名拿出來(lái)if p in proc_name:print("%s啟動(dòng)成功"%p)p_status = '啟動(dòng)'data = {"msgtype": "markdown","markdown": {"title":"監(jiān)控訊息","text": "#### %s\n" % time_now +"> ##### 服務(wù)名: %s \n\n" % p_name +"> ##### 狀態(tài): %s \n" % p_status +"> ##### 重啟成功"}}headers = {'Content-Type':'application/json ; charset=UTF-8'}send_data = json.dumps(data).encode('utf-8')requests.post(url=url,data=send_data,headers=headers)else:print("%s啟動(dòng)失敗"%p)p_status = '停止'data = {"msgtype": "markdown","markdown": {"title":"監(jiān)控訊息","text": "#### %s\n" % time_now +"> ##### 服務(wù)名: %s \n\n" % p_name +"> ##### 狀態(tài): %s \n" % p_status +"> ##### 啟動(dòng)失敗"}}headers = {'Content-Type':'application/json ; charset=UTF-8'}send_data = json.dumps(data).encode('utf-8')requests.post(url=url,data=send_data,headers=headers)time.sleep(2)if __name__ == '__main__':while True:process_message()
轉(zhuǎn)載于:https://www.cnblogs.com/liu66blog/p/8450128.html
總結(jié)
以上是生活随笔為你收集整理的[Python Study Notes]进程信息(丁丁软件监控进程,http-post)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Kotlin之集合遍历
- 下一篇: 华为OD机试真题大全,用 Python