Skip to content

Commit 1a637a7

Browse files
author
root
committed
py
1 parent 350a7d0 commit 1a637a7

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

back_project.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/python
2+
import os
3+
import time
4+
os.chdir('/project/')
5+
source_list = ['/project/app','/project/rqb','/project/rqt']
6+
target_dir = '/backup/'
7+
today = time.strftime('%Y%m%d')
8+
if not os.path.exists(target_dir + today):
9+
os.mkdir(target_dir + today)
10+
print "Successful created directory",target_dir + today
11+
for source in source_list:
12+
target = target_dir + today + os.sep + source[9:] + '.tar.gz'
13+
tar_command = "tar zcvf %s %s" % (target, source[9:])
14+
15+
if os.system(tar_command) == 0:
16+
print 'Successful backup to',target
17+
time.sleep(3)
18+
else:
19+
print 'Backup FALLED'

moitor_log.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
# Filename: monitorLog.py
4+
# Author: gujiwork@outlook.com
5+
# This is a real-time monitoring logs, the script for the specified keyword alarm
6+
7+
import subprocess
8+
import sys
9+
import urllib
10+
import urllib2
11+
import os
12+
import signal
13+
import time
14+
15+
#短信接口
16+
def sendsms(mobile,content):
17+
URL = 'SMS interface url'
18+
content = '[%s] %s' % (time.strftime('%Y%m%d %H:%M:%S'),content)
19+
data = {'m':mobile,'c':content}
20+
body = urllib.urlencode(data)
21+
request = urllib2.Request(URL,body)
22+
urldata = urllib2.urlopen(request)
23+
24+
25+
#日志文件目录和名称
26+
logFile = '/usr/local/tomcat8/logs/name.log'
27+
28+
29+
def monitorLog(logFile):
30+
print '监控的日志文件 是%s' % logFile
31+
popen = subprocess.Popen('tail -f ' + logFile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
32+
pid = popen.pid
33+
print('Popen.pid:' + str(pid))
34+
35+
while True:
36+
line = popen.stdout.readline().strip()
37+
38+
# 判断内容是否为空,避免在重启tomcat,杀掉日志
39+
if len(line) == 0:
40+
print 'tail进程死掉'
41+
popen.kill()
42+
print '杀掉进程,重新执行程序'
43+
break
44+
# 发送报警人
45+
if 'org.hibernate.exception.LockTimeoutException' in line:
46+
print('发现异常报警')
47+
sendsms(15066666666,'级别:严重 LockTimeoutException错误')
48+
49+
50+
# 当前时间
51+
thistime = time.strftime('%H:%M')
52+
if thistime == '23:59':
53+
popen.kill()
54+
sys.exit('清空僵尸进程')
55+
56+
print '等待180秒'
57+
time.sleep(180)
58+
monitorLog(logFile)
59+
60+
if __name__ == '__main__':
61+
monitorLog(logFile)

moitor_sql_sms.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*-coding:utf-8-*-
3+
# Filename: monitorSQL.py
4+
# Author: gujiwork@outlook.com
5+
# This is a real-time monitoring logs, the script for the specified keyword alarm
6+
import pymysql
7+
import sys
8+
import urllib
9+
import urllib2
10+
import time
11+
12+
13+
#短信接口
14+
URL = 'SMS interface url'
15+
def sendsms(mobile,content):
16+
content = '[%s] %s' % (time.strftime('%Y%m%d %H:%M:%S'),content)
17+
data = {'m':mobile,'c':content}
18+
body = urllib.urlencode(data)
19+
request = urllib2.Request(URL,body)
20+
urldata = urllib2.urlopen(request)
21+
22+
#打开数据库
23+
db = pymysql.connect('192.168.1.1','username','password','DBname',charset='utf8')
24+
25+
#使用cursor()方法创建一个游标对象cursor
26+
cursor = db.cursor()
27+
28+
sql = "Input to sql"
29+
try:
30+
#获取SQL语句
31+
cursor.execute(sql)
32+
#获取所有记录列表
33+
results = cursor.fetchall()
34+
len_num = len(results) #结果条数
35+
if len_num > 0:
36+
#设置报警人手机
37+
sendsms(15066666666,'SQL执行结果为%s条'%len_num)
38+
else:
39+
pass
40+
except:
41+
print 'Error:unable to fecth data'
42+
43+
db.close()

0 commit comments

Comments
 (0)