[Python Study Notes]進程資訊(丁丁軟體監控進程,http-post)

來源:互聯網
上載者:User

標籤:啟動失敗   import   def   lap   outlook   bin   ica   time   util   

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘>>檔案: 進程資訊.py>>作者: liu yang>>郵箱: [email protected]‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘#!/usr/bin/env python# -*- coding: utf-8 -*-import sys,osimport psutil# http get/postimport requests# 處理字串import json# 擷取系統時間import time# 丁丁urlurl = ‘https://oapi.dingtalk.com/robot/send?access_token=3d94c740a27d5f70da6acf2a1c9383b338ad5c562fbaece8d38595c405e7e6a9‘# 轉換成當前的時間戳記time_now = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))# 要監控的進程monitor_name = {‘ufw‘}# 要監控的進程,與shell語句對應,先定義為空白monitor_map={}#要監控的服務名集合,{‘ufw‘: ‘service ufw start‘}for m in monitor_name:    monitor_map[m]=‘service ‘+m+‘ start‘print(monitor_map)# print(time_now)# 顯示進程,分別得到進程字典{‘pid‘:‘name‘}和去重元祖()def process_message():    # 進程字典,儲存pid,進程名的索引值對    proc_dict={}    # 進程列表,儲存去重的進程名稱    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:提取出來哪一個進程沒有在當前作業系統下開啟    if proc_stop:        #當前沒有啟動的這個進程集合不為空白,有服務死掉了        for p in proc_stop:            print("正在重新啟動%s"%p)            #取出每一個沒有啟動的服務名            p_status = ‘停止‘            p_name = p            data = {                "msgtype": "markdown",                "markdown": {                "title":"監控訊息",                "text": "#### %s\n" % time_now +                 "> ##### 服務名: %s \n\n" % p_name +                 "> ##### 狀態: %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)            #post提交資料:提交的地址,提交的資料            if sys.platform.lower()==‘linux‘:                os.system(monitor_map[p])            #函數不會給你傳回值            proc_name = set()            for p2 in psutil.process_iter(attrs=[‘pid‘,‘name‘]):                proc_name.add(p2.info[‘name‘])                #作業系統下的所有進程名拿出來            if p in proc_name:                print("%s啟動成功"%p)                p_status = ‘啟動‘                data = {                    "msgtype": "markdown",                    "markdown": {                    "title":"監控訊息",                    "text": "#### %s\n" % time_now +                     "> ##### 服務名: %s \n\n" % p_name +                     "> ##### 狀態: %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啟動失敗"%p)                p_status = ‘停止‘                data = {                    "msgtype": "markdown",                    "markdown": {                    "title":"監控訊息",                    "text": "#### %s\n" % time_now +                     "> ##### 服務名: %s \n\n" % p_name +                     "> ##### 狀態: %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)    time.sleep(2)if __name__ == ‘__main__‘:    while True:        process_message()

[Python Study Notes]進程資訊(丁丁軟體監控進程,http-post)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.