使用Python寫(xiě)個(gè)小監(jiān)控
1.入門(mén)
首先你得用過(guò)C/C++、java、Javascript等的一種,編程小白估計(jì)比較艱難,有一定編程經(jīng)驗(yàn)的python小白相對(duì)簡(jiǎn)單些。
1.1 Hello World!
Python安裝比較簡(jiǎn)單,到官網(wǎng)上下載安裝包,一路下一步就可以了。因?yàn)槲业姆?wù)器上安裝的是2.6.6,所以我也下了這個(gè)版本。話說(shuō)2.x的差別不是很大,如果想用3.x,可能下面的代碼直接運(yùn)行不過(guò),不過(guò)也差不多,稍微改改即可。
新建一個(gè)文件,命名為hello.py。使用python的IDLE打開(kāi)hello.py,寫(xiě)入以下代碼:
print "Hello World!"
按F5,就可以看見(jiàn)輸出結(jié)果了。
1.2 基本語(yǔ)法
每一行是一條語(yǔ)句。C語(yǔ)言是通過(guò)分號(hào)”;“;
通過(guò)縮進(jìn)來(lái)組織代碼塊。C語(yǔ)言是通過(guò)大括號(hào)”{}“;
注釋使用井號(hào)”#“。
1.3 數(shù)據(jù)類(lèi)型、運(yùn)算符、數(shù)據(jù)結(jié)構(gòu)
運(yùn)算符和C語(yǔ)言差不多,C語(yǔ)言有的基本上直接用就可以。
數(shù)據(jù)類(lèi)型有數(shù)值型,字符串。數(shù)據(jù)結(jié)構(gòu)有 list, tuple, dict, set。介紹一下tuple, 不能修改,通過(guò)索引進(jìn)行查找。dict類(lèi)似于map,存放鍵值對(duì)。來(lái)看例子,看看tuple使用:
>>> t=(1,2,[1,2]) >>> t[2] [1, 2]
1.4 流程控制
Python中可以使用if elif else、for和 while 來(lái)實(shí)現(xiàn)流程控制。同樣有 break 和 continue。有一點(diǎn)和C不同,如果有一個(gè)分支什么都不做,要使用 pass。例如
list=[0, 1, 2, 3, 4, 5]
for item in list:
if item == 1:
print item
elif item in (2, 3, 4, 5):
print "aha " + str(item)
else:
pass
運(yùn)行結(jié)果是:
1
aha 2
aha 3
aha 4
aha 5
1.5 模塊組織
有方法和類(lèi)。
方法這樣定義
def func(var): some code here
類(lèi)和C++等有些不同
class MyClass(object):
common = 1
def __init__(self):
self.myvariable = 5
def myfunction(self, arg1, arg2):
return self.myvariable
common變量相當(dāng)于C++中用 static 修飾的變量,所有類(lèi)通用;繼承也非常簡(jiǎn)單,可以看看開(kāi)始推薦的那篇文章。
1.6 異常處理
異常處理非常簡(jiǎn)單,直接貼代碼了:
def some_function():
try:
# Division by zero raises an exception
10 / 0
except ZeroDivisionError:
print "Oops, invalid."
else:
# Exception didn't occur, we're good.
pass
finally:
# This is executed after the code block is run
# and all exceptions have been handled, even
# if a new exception is raised while handling.
print "We're done with that."
1.7 工程組織
直接引用庫(kù),或者從庫(kù)中引入某一個(gè)方法或變量。
import random from time import clock
2. 數(shù)據(jù)庫(kù)查詢(xún)
既然是監(jiān)控,免不了和數(shù)據(jù)庫(kù)打交道。我使用的是PostgreSQL,所以就介紹一下python怎么調(diào)用postgres。
連接postgres首先要安裝一個(gè)庫(kù)psycopg2,Windows下直接下載安裝即可,注意選對(duì)版本。我的服務(wù)器是CentOS,安裝直接運(yùn)行
yum install python-psycopg2
就OK了。
2.1 首先創(chuàng)建數(shù)據(jù)庫(kù)連接
#get database connect def get_con(): host = '127.0.0.1' port = "5432" database = 'platform' user = 'postgres' password = 'postgres' conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port) return conn
2.2 執(zhí)行SQL語(yǔ)句
#執(zhí)行sql查詢(xún) def query(conn, sql): cursor = conn.cursor() cursor.execute(sql) results = cursor.fetchall() #close cursor cursor.close() return results
2.3 然后就可以寫(xiě)具體業(yè)務(wù)了
def getUsers():
conn = get_con()#open connect
sql = """select *
from t_user
order by intime DESC
limit 5"""
items = query(conn , sql)
print str(items)
conn.close() #close connect
注意3個(gè)引號(hào)”””,就是普通字符串,不過(guò)可以換行。
3. 發(fā)送郵件
查詢(xún)到數(shù)據(jù)之后不能及時(shí)通知管理員的話監(jiān)控就沒(méi)有意義了。所以我們通過(guò)郵件來(lái)通知,直接使用python的標(biāo)準(zhǔn)庫(kù) smtplib 就可以了。寫(xiě)個(gè)發(fā)送郵件的函數(shù):
#發(fā)送郵件
def send_email(subject, content):
sender = "yourmail@***.com"
password = "******" #密碼是看不見(jiàn)的哦
receivers = [tq8117179#163.com] #本人真實(shí)郵箱,歡迎發(fā)郵件討論技術(shù)問(wèn)題
host = "smtp.exmail.qq.com"
port = 465
msg = MIMEText(content,'html','utf-8')
msg['From'] = sender
msg['To'] = ",".join(receivers)
msg['Subject'] = Header(subject, 'utf-8')
try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, password)
smtp.sendmail(sender, receivers, msg.as_string())
except Exception, e:
logger.error(e)
logger.info(content)
4.日志
發(fā)送郵件時(shí)我們使用了logger,這個(gè)logger是怎么來(lái)的呢?新建一個(gè)log.py,代碼如下
# coding=utf-8
import logging
import logging.handlers
logger = logging.getLogger('monitor')
logger.setLevel(logging.DEBUG)
filehandler = logging.handlers.TimedRotatingFileHandler(
"/mnt/log/monitor/monitor_log", 'midnight', 1, 7)
# 設(shè)置文件后綴名稱(chēng)
filehandler.suffix = "%Y%m%d.log"
formatter = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s: %(message)s')
filehandler.setFormatter(formatter)
logger.addHandler(filehandler)
通過(guò)logging.getLogger(‘monitor')生成一個(gè)logger,然后配置一個(gè)文件處理器。
然后在我們監(jiān)控程序中引用即可:
from log import logger
5. 把可配置信息放到配置文件中
如果我們添加一個(gè)管理員怎么辦?如果我們的郵箱密碼變了怎么辦?直接修改python文件啊,哈哈。python不用編譯直接改代碼就好了,可是我們的程序以后要打包呢,所以最好寫(xiě)個(gè)配置文件,python的配置文件讀取非常簡(jiǎn)單,使用python庫(kù) ConfigParser 即可:
config = None
#get config
def getConfig():
global config
if config is None:
config = ConfigParser.ConfigParser()
config.read("monitor.ini")
return config
然后這樣使用:
#get database connect
def get_con():
host = getConfig().get('db', 'host')
port = getConfig().get('db', 'port')
database = getConfig().get('db', 'database')
user = getConfig().get('db', 'user')
password = getConfig().get('db', 'password')
conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port)
return conn
#發(fā)送郵件
def send_email(subject, content):
sender = getConfig().get('mail', 'sender')
password = getConfig().get('mail', 'password')
receivers = getConfig().get('mail', 'receivers').split(",")
host = getConfig().get('mail', 'host')
port = getConfig().getint('mail', 'port')
msg = MIMEText(content,'html','utf-8')
msg['From'] = sender
msg['To'] = ",".join(receivers)
msg['Subject'] = Header(subject, 'utf-8')
try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, password)
smtp.sendmail(sender, receivers, msg.as_string())
except:
logger.exception("Exception: ")
logger.info(content)
配置文件是monitor.ini,內(nèi)容如下:
#數(shù)據(jù)庫(kù)配置 [db] host = 127.0.0.1 port = 5432 database = platform user = postgres password = postgres #郵件配置 [mail] sender = yourmail@XXX.com password = ****** #多個(gè)聯(lián)系人用英文逗號(hào)隔開(kāi) receivers = tq8117179#163.com host = smtp.exmail.qq.com port = 465
6. 加點(diǎn)控制
我們每5分鐘查一下數(shù)據(jù),可是業(yè)務(wù)sql只能查詢(xún)最近的幾條,所以要加個(gè)時(shí)間段限制,弄個(gè)開(kāi)始、結(jié)束時(shí)間。
start_time = "2015-10-1 16:24:24"
end_time = None
#update end_time, invoke before get new data
def update_end_time():
global end_time
now = time.mktime(datetime.now().timetuple())
end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
return end_time
#update end_time, invoke after get new data
def update_start_time():
global start_time
global end_time
start_time = end_time
return start_time
getUsers可以改寫(xiě)成:
def getUsers (conn):
global start_time
global end_time
sql = """select *
from t_user
where intime>=""" +"'"+start_time+"' and intime<"+"'"+end_time+"';"
items = query(conn, sql)
if items is not None and len(items)>0:
count = len(items)
tip = "又有"+str(count)+"個(gè)用戶(hù)已經(jīng)注冊(cè)了。"+end_time
send_email(tip, tip+"\n"+str(items))
然后寫(xiě)個(gè)統(tǒng)一的調(diào)度:
def task():
#init end_time and start_time, must init end_time first!!!
end_time = update_end_time()
start_time = update_start_time()
#init config
getConfig()
while True:
conn = get_con() #open connect
end_time = update_end_time()
############## process ##############
logger.info("query: "+end_time)
getUsers (conn)
#do some task else here
## end
update_start_time()
conn.close()#close connect
time.sleep(5*60)
#end of while
def run_monitor():
monitor = threading.Thread(target=task)
monitor.start()
if __name__ == "__main__":
run_monitor()
在task這個(gè)函數(shù)的while中,首先更新end_time,也就是當(dāng)前時(shí)間;執(zhí)行完再把start_time更新成剛剛的end_time,這樣就不會(huì)有漏網(wǎng)之魚(yú)了。還有一個(gè)需要注意的地方,關(guān)鍵字global。 在python中,使用全局變量是需要global關(guān)鍵字進(jìn)行聲明的,否則會(huì)出問(wèn)題。
7. 運(yùn)行
打開(kāi)linux 控制臺(tái),直接運(yùn)行python monitor.py是可以運(yùn)行的,可是shell一旦退出,任務(wù)也就停止了。于是我就選擇了一個(gè)進(jìn)程管理工具:Supervisor。Supervisor 在進(jìn)程中斷時(shí)還能自動(dòng)重啟。
7.1. 安裝supervisor
首先安裝python-setuptools
yum install python-setuptools
安裝supervisor
easy_install supervisor
生成supervisor配置文件
echo_supervisord_conf > /etc/supervisord.conf
然后在/etc/supervisord.conf添加:
[program:monitor] command = python /usr/monitor/monitor.py directory = /usr/monitor user = root
7.2. 運(yùn)行監(jiān)控
然后在終端中運(yùn)行supervisord啟動(dòng)supervisor。
在終端中運(yùn)行supervisorctl,進(jìn)入shell,運(yùn)行status查看腳本的運(yùn)行狀態(tài)。
7.3. 關(guān)閉監(jiān)控 以及常用命令
以下命令全部在supervisorctl的shell中執(zhí)行。
- shutdown 停止Supervisor(子進(jìn)程也會(huì)被停止) ;
- start monitor 開(kāi)啟monitor進(jìn)程服務(wù)(一旦monitor進(jìn)程退出,會(huì)自啟動(dòng)) ;
- stop monitor 關(guān)閉monitor進(jìn)程服務(wù) ;
- restart monitor 關(guān)閉正在運(yùn)行的monitor進(jìn)程,并且重新啟動(dòng)monitor進(jìn)程服務(wù) ;
- reload 重新加載supervisor配置文件 ;
- exit 退出supervisorctl的shell。
程序基本上就寫(xiě)完了,也可以跑起來(lái)了,是不是很酷,大家快點(diǎn)動(dòng)手實(shí)踐一下吧!
- 寫(xiě)了個(gè)監(jiān)控nginx進(jìn)程的Python腳本
- python動(dòng)態(tài)監(jiān)控日志內(nèi)容的示例
- python操作攝像頭截圖實(shí)現(xiàn)遠(yuǎn)程監(jiān)控的例子
- python實(shí)現(xiàn)監(jiān)控windows服務(wù)并自動(dòng)啟動(dòng)服務(wù)示例
- python監(jiān)控網(wǎng)卡流量并使用graphite繪圖的示例
- 使用Python的Supervisor進(jìn)行進(jìn)程監(jiān)控以及自動(dòng)啟動(dòng)
- python實(shí)現(xiàn)監(jiān)控linux性能及進(jìn)程消耗性能的方法
- Python寫(xiě)的服務(wù)監(jiān)控程序?qū)嵗?/a>
- Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控
- Python中使用Inotify監(jiān)控文件實(shí)例
相關(guān)文章
10個(gè)Python常用的損失函數(shù)及代碼實(shí)現(xiàn)分享
損失函數(shù)是一種衡量模型與數(shù)據(jù)吻合程度的算法。損失函數(shù)測(cè)量實(shí)際測(cè)量值和預(yù)測(cè)值之間差距的一種方式。本文為大家總結(jié)了10個(gè)常用的損失函數(shù)及Python代碼實(shí)現(xiàn),需要的可以參考一下2022-09-09
Python中關(guān)于列表的常規(guī)操作范例以及介紹
列表是一種有序的集合,可以隨時(shí)添加和刪除其中的元素。在python中使用的頻率非常高,本篇文章對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09
Python虛擬環(huán)境virtualenv是如何使用的
今天給大家?guī)?lái)的是關(guān)于Python虛擬環(huán)境的相關(guān)知識(shí),文章圍繞著Python虛擬環(huán)境virtualenv是如何使用的展開(kāi),文中有非常詳細(xì)的解釋及代碼示例,需要的朋友可以參考下2021-06-06
基于Python+Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
Tkinter作為Python的標(biāo)準(zhǔn)庫(kù),是非常流行的Python GUI工具,同時(shí)也是非常容易學(xué)習(xí)的。本文將利用Tkinter繪制一個(gè)簡(jiǎn)單的計(jì)算器,感興趣的可以試一試2022-01-01
Python中venv虛擬環(huán)境超詳細(xì)講解
虛擬環(huán)境是一個(gè)獨(dú)立的Python環(huán)境,它與系統(tǒng)的全局Python環(huán)境隔離,這篇文章主要介紹了Python中venv虛擬環(huán)境的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
Python 如何將字符串每?jī)蓚€(gè)用空格隔開(kāi)
這篇文章主要介紹了Python Python 如何將字符串每?jī)蓚€(gè)用空格隔開(kāi)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
使用Python實(shí)現(xiàn)視頻封面批量下載器
在視頻網(wǎng)站上,每個(gè)視頻都有一個(gè)獨(dú)特的封面圖像,本文主要為大家詳細(xì)如何使用Python編寫(xiě)一個(gè)視頻封面批量下載器,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
詳解Python圖像處理中內(nèi)存泄漏的問(wèn)題解決方法
在Python編程中,尤其是在圖像處理領(lǐng)域,內(nèi)存泄漏是一個(gè)不容忽視的問(wèn)題,本文將深入探討Python為何容易發(fā)生內(nèi)存泄漏,以及如何有效檢測(cè)和解決,希望對(duì)大家有所幫助2025-02-02

