導航:首頁 > 編程語言 > python性能監控工具

python性能監控工具

發布時間:2022-08-03 20:14:53

1. 如何監控在伺服器上運行的多個python腳本的狀態

linux系統的話,監控工具比較好的有ganglia,zabbix
windows系統的話,用自帶的「性能監視器」(老版本的windows叫性能計數器)

2. 有python開發的監控工具嗎

在公司里做的一個介面系統,主要是對接第三方的系統介面,所以,這個系統里會和很多其他公司的項目交互。隨之而來一個很蛋疼的問題,這么多公司的介面,不同公司介面的穩定性差別很大,訪問量大的時候,有的不怎麼行的介面就各種出錯了。
這個介面系統剛剛開發不久,整個系統中,處於比較邊緣的位置,不像其他項目,有日誌庫,還有簡訊告警,一旦出問題,很多情況下都是用戶反饋回來,所以,我的想法是,拿起python,為這個項目寫一個監控。如果在調用某個第三方介面的過程中,大量出錯了,說明這個介面有有問題了,就可以更快的採取措施。
項目的也是有日誌庫的,所有的info,error日誌都是每隔一分鍾掃描入庫,日誌庫是用的mysql,表裡有幾個特別重要的欄位:
有日誌庫,就不用自己去線上環境掃日誌分析了,直接從日誌庫入手。由於日誌庫在線上時每隔1分鍾掃,那我就去日誌庫每隔2分鍾掃一次,如果掃到有一定數量的error日誌就報警,如果只有一兩條錯誤就可以無視了,也就是短時間爆發大量錯誤日誌,就可以斷定系統有問題了。報警方式就用發送郵件,所以,需要做下面幾件事情:
1. 操作MySql。
2. 發送郵件。
3. 定時任務。
4. 日誌。
5. 運行腳本。
明確了以上幾件事情,就可以動手了。
操作資料庫
使用MySQLdb這個驅動,直接操作資料庫,主要就是查詢操作。
獲取資料庫的連接:

def get_con():
host = "127.0.0.1"
port = 3306
logsdb = "logsdb"
user = "root"
password = "never tell you"
con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8")
return con

從日誌庫里獲取數據,獲取當前時間之前2分鍾的數據,首先,根據當前時間進行計算一下時間。之前,計算有問題,現在已經修改。

def calculate_time():

now = time.mktime(datetime.now().timetuple())-60*2
result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
return result

然後,根據時間和日誌級別去日誌庫查詢數據

def get_data():
select_time = calculate_time()
logger.info("select time:"+select_time)
sql = "select file_name,message from logsdb.app_logs_record " \
"where log_time >"+"'"+select_time+"'" \
"and level="+"'ERROR'" \
"order by log_time desc"
conn = get_con()

cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()

cursor.close()
conn.close()

return results

發送郵件
使用python發送郵件比較簡單,使用標准庫smtplib就可以
這里使用163郵箱進行發送,你可以使用其他郵箱或者企業郵箱都行,不過host和port要設置正確。

def send_email(content):

sender = "[email protected]"
receiver = ["[email protected]", "[email protected]"]
host = 'smtp.163.com'
port = 465
msg = MIMEText(content)
msg['From'] = "[email protected]"
msg['To'] = "[email protected],[email protected]"
msg['Subject'] = "system error warning"

try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, '123456')
smtp.sendmail(sender, receiver, msg.as_string())
logger.info("send email success")
except Exception, e:
logger.error(e)

定時任務
使用一個單獨的線程,每2分鍾掃描一次,如果ERROR級別的日誌條數超過5條,就發郵件通知。

def task():
while True:
logger.info("monitor running")

results = get_data()
if results is not None and len(results) > 5:
content = "recharge error:"
logger.info("a lot of error,so send mail")
for r in results:
content += r[1]+'\n'
send_email(content)
sleep(2*60)

日誌
為這個小小的腳本配置一下日誌log.py,讓日誌可以輸出到文件和控制台中。
# coding=utf-8
import logging

logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler('monitor.log')
fh.setLevel(logging.INFO)

ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

所以,最後,這個監控小程序就是這樣的app_monitor.py
# coding=utf-8
import threading
import MySQLdb
from datetime import datetime
import time
import smtplib
from email.mime.text import MIMEText
from log import logger

def get_con():
host = "127.0.0.1"
port = 3306
logsdb = "logsdb"
user = "root"
password = "never tell you"
con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8")
return con

def calculate_time():

now = time.mktime(datetime.now().timetuple())-60*2
result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
return result

def get_data():
select_time = calculate_time()
logger.info("select time:"+select_time)
sql = "select file_name,message from logsdb.app_logs_record " \
"where log_time >"+"'"+select_time+"'" \
"and level="+"'ERROR'" \
"order by log_time desc"
conn = get_con()

cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()

cursor.close()
conn.close()

return results

def send_email(content):

sender = "[email protected]"
receiver = ["[email protected]", "[email protected]"]
host = 'smtp.163.com'
port = 465
msg = MIMEText(content)
msg['From'] = "[email protected]"
msg['To'] = "[email protected],[email protected]"
msg['Subject'] = "system error warning"

try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, '123456')
smtp.sendmail(sender, receiver, msg.as_string())
logger.info("send email success")
except Exception, e:
logger.error(e)

def task():
while True:
logger.info("monitor running")
results = get_data()
if results is not None and len(results) > 5:
content = "recharge error:"
logger.info("a lot of error,so send mail")
for r in results:
content += r[1]+'\n'
send_email(content)
time.sleep(2*60)

def run_monitor():
monitor = threading.Thread(target=task)
monitor.start()

if __name__ == "__main__":
run_monitor()

運行腳本
腳本在伺服器上運行,使用supervisor進行管理。
在伺服器(centos6)上安裝supervisor,然後在/etc/supervisor.conf中加入一下配置:

復制代碼代碼如下:
[program:app-monitor]
command = python /root/monitor/app_monitor.py
directory = /root/monitor
user = root

然後在終端中運行supervisord啟動supervisor。
在終端中運行supervisorctl,進入shell,運行status查看腳本的運行狀態。
總結
這個小監控思路很清晰,還可以繼續修改,比如:監控特定的介面,發送簡訊通知等等。
因為有日誌庫,就少了去線上正式環境掃描日誌的麻煩,所以,如果沒有日誌庫,就要自己上線上環境掃描,在正式線上環境一定要小心哇~

3. 如何實現Python伺服器性能監控

其實你完全可以使用現成的工具:linux系統的話,監控工具比較好的有ganglia,zabbix windows系統的話,用自帶的「性能監視器」(老版本的windows叫性能計數器)

4. 請問大佬有Glances(硬體監控工具) V3.1.7 官方版軟體免費百度雲資源嗎

鏈接:

提取碼:468s

軟體名稱:Glances(硬體監控工具)V3.1.7官方版

語言:簡體中文

大小:6.74MB

類別:系統工具

介紹:Glances是一個跨平台的監控工具,glances是一個基於python語言開發,可以為linux或者UNIX性能提供監視和分析性能數據的功能。glances在用戶的終端上顯示重要的系統信息,並動態的進行更新,讓管理員實時掌握系統資源的使用情況!

5. 如何Zabbix和Python腳本批量監控網站性能指標

帶界面的工具 1、MySQL可視化工具 這些工具都可以免費使用: a、MySQL查詢瀏覽器(MySQL Query Browser):這個不用說了… b、MySQL管理員(MySQL Administrator):功能集中在伺服器管理上,所以它最適合DBA使用,

6. 如何用python編寫一個伺服器狀態監控腳本

其實你完全可以使用現成的工具:
linux系統的話,監控工具比較好的有ganglia,zabbix
windows系統的話,用自帶的「性能監視器」(老版本的windows叫性能計數器)

7. 怎麼用python實現電腦cpu溫度監控

def monitor_process(key_word, cmd):
p1 = subprocess.Popen(['ps',
'-ef'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', key_word],
stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['grep',
'-v', 'grep'], stdin=p2.stdout, stdout=subprocess.PIPE)

lines = p3.stdout.readlines()
if len(lines) > 0:

return

sys.stderr.write('process[%s] is lost, run [%s]\n' % (key_word,
cmd))
subprocess.call(cmd, shell=True)

8. 使用python,在linux上監控遠程windows的CPU、硬碟、內存使用率

你需要安裝wmic,它實現了linux下能使用wmi,安裝以後就可以用了,下面是例子。
import wmi_client_wrapper as wmi
wmic = wmi.WmiClientWrapper(
username="Administrator",
password="password",
host="192.168.1.149",
)
output = wmic.query("SELECT * FROM Win32_Processor")

9. 如何用python做一個設備運維軟體

Python開發的jumpserver跳板機

使用python語言編寫的調度和監控工作流的平台內部用來創建、監控和調整數據管道。任何工作流都可以在這個使用Python來編寫的平台上運行。

企業主要用於解決:通俗點說就是規范運維的操作,加入審批,一步一步操作的概念。

是一種允許工作流開發人員輕松創建、維護和周期性地調度運行工作流(即有向無環圖或成為DAGs)的工具。這些工作流包括了如數據存儲、增長分析、Email發送、A/B測試等等這些跨越多部門的用例。

這個平台擁有和 Hive、Presto、MySQL、HDFS、Postgres和S3交互的能力,並且提供了鉤子使得系統擁有很好地擴展性。除了一個命令行界面,該工具還提供了一個基於Web的用戶界面讓您可以可視化管道的依賴關系、監控進度、觸發任務等。

來個小總結

10. 怎樣對持續運行的python程序進行性能分析監控

不可能吧,你可以寫一個類似於django中間件的東西做性能分析,然後將結果寫入日誌
這樣做容易一些

閱讀全文

與python性能監控工具相關的資料

熱點內容
java迭代器遍歷 瀏覽:299
閩政通無法請求伺服器是什麼 瀏覽:48
怎麼做積木解壓神器 瀏覽:203
王者榮耀解壓玩具抽獎 瀏覽:49
12位是由啥加密的 瀏覽:868
程序員編迷你世界代碼 瀏覽:895
php取現在時間 瀏覽:246
單片機高吸收 瀏覽:427
怎麼區分五代頭是不是加密噴頭 瀏覽:244
hunt測試伺服器是什麼意思 瀏覽:510
2013程序員考試 瀏覽:641
畢業論文是pdf 瀏覽:736
伺服器跑網心雲劃算嗎 瀏覽:471
單片機定時器計數初值的計算公式 瀏覽:801
win7控制台命令 瀏覽:567
貓咪成年app怎麼升級 瀏覽:692
360有沒有加密軟體 瀏覽:315
清除cisco交換機配置命令 瀏覽:751
華為刪除交換機配置命令 瀏覽:473
shell打包命令 瀏覽:827