OS : ubuntu 14.04 LTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import subprocess
import commands
import time
import mysql.connector # apt-get install python-mysql.connector
import socket
 
'''
[ smartctl 설치 ]
apt-get install smartmontools 
[ mysql 테이블 ]
CREATE TABLE `hdd`
(
    `idx` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `host` VARCHAR(64) NOT NULL DEFAULT '',
    `device` VARCHAR(64) NOT NULL DEFAULT '',
    `stat` VARCHAR(64) NOT NULL DEFAULT '',
    `insertDateTime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`idx`),
    UNIQUE INDEX `idx_UNIQUE` (`idx` ASC)
) ENGINE = InnoDB;
CREATE TABLE `hddMin`
(
    `idx` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `host` VARCHAR(64) NOT NULL DEFAULT '',
    `device` VARCHAR(64) NOT NULL DEFAULT '',
    `stat` VARCHAR(64) NOT NULL DEFAULT '',
    `insertDateTime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`idx`),
    UNIQUE INDEX `idx_UNIQUE` (`idx` ASC)
) ENGINE = InnoDB;
'''
 
'''
중복실행 방지코드
'''
isRun = commands.getstatusoutput('ps -aux | grep "' + __file__ + '" | grep -v "grep" | awk "{print \$2}" | wc -l')
 
if int(isRun[1]) > 1:
    print "already run"
    sys.exit()
 
'''
라이브러리
'''
 
def mysqlConnect():
    if hasattr(mysqlConnect, 'conn'== False or mysqlConnect.conn.is_connected() == False:
        mysqlConnect.conn = mysql.connector.connect(user='id', password='pw', host='host', database='server')
        # mysqlConnect.conn = mysql.connector.connect(user='id', password='pw', host='192.168.0.22', database='server')
        mysqlConnect.cursor = mysqlConnect.conn.cursor(buffered=True)
 
    return mysqlConnect
 
def mysqlQuery(queryStr):
    print queryStr
 
    mysqlInit = mysqlConnect();
    mysqlInit.cursor.execute(queryStr)
    mysqlInit.conn.commit()
 
    return mysqlInit.cursor
 
 
def mysqlQueryFetchOne(queryStr):
 
    cursor = mysqlQuery(queryStr)
 
    fetch = cursor.fetchone()
 
    if fetch == None:
        return None
 
    desc = cursor.description
 
    returnArr = {}
 
    for (name, value) in zip(desc, fetch):
        returnArr[name[0]] = value
 
    return returnArr
 
 
def strArrConcat(strArr):
    from cStringIO import StringIO
 
    strIo = StringIO()
 
    for string in strArr:
        strIo.write(str(string))
 
    return strIo.getvalue()
 
 
 
'''
환경변수
'''
envHostName = socket.gethostname()
envNow = time.localtime()
envDateTime = strArrConcat([
    envNow[0]
    ,'-'
    ,envNow[1]
    ,'-'
    ,envNow[2]
    ,' '
    ,envNow[3]
    ,':'
    ,envNow[4]
    ,':00'
    #,envNow[5]
])
 
 
'''
smartctl을 이용해 하드디스크 검사함
'''
smartctlScan = commands.getstatusoutput('smartctl --scan-open')
smartctlScanResult = smartctlScan[1];
smartctlScanArr = smartctlScanResult.split('\n')
 
smartctlCheckDic = {}
smartctlCheckDic.clear()
 
for targetStr in smartctlScanArr:
    if targetStr[:1!= '#':
        target = targetStr[:targetStr.find('#')].strip()
 
        smartctlCheck = commands.getstatusoutput('smartctl -H ' + target)
 
        print smartctlCheck[1]
 
        if smartctlCheck[1].find('failed'> 0:
            smartctlCheckDic[target] = "failed"
 
        elif smartctlCheck[1].find('OK'> 0:
            smartctlCheckDic[target] = "OK"
 
        elif smartctlCheck[1].find('PASSED'> 0:
            smartctlCheckDic[target] = "PASSED"
 
        elif smartctlCheck[1].find('FAILED'> 0:
            smartctlCheckDic[target] = "FAILED"
 
        else:
            smartctlCheckDic[target] = "unknown"
 
print smartctlCheckDic
 
for smartctlCheckDevice in smartctlCheckDic:
    smartctlCheckResult = smartctlCheckDic[smartctlCheckDevice]
 
    mysqlQuery(strArrConcat([
        "DELETE FROM"
        ," `hdd` "
 
        ," WHERE "
        ,"`host` = '"
        ,envHostName
        ,"'"
        ," AND `device` = '"
        ,smartctlCheckDevice
        ,"'"
    ]))
 
    mysqlQuery(strArrConcat([
        "INSERT INTO"
        ," `hdd` "
 
        ," SET "
        ,"`host` = '"
        ,envHostName
        ,"'"
        ,", `device` = '"
        ,smartctlCheckDevice
        ,"'"
        ,",`stat` = '"
        ,smartctlCheckResult
        ,"'"
        ,",`insertDateTime` = '"
        ,envDateTime
        ,"'"
    ]))
 
    mysqlQuery(strArrConcat([
        "INSERT INTO"
        ," `hddMin` "
 
        ," SET "
        ,"`host` = '"
        ,envHostName
        ,"'"
        ,", `device` = '"
        ,smartctlCheckDevice
        ,"'"
        ,",`stat` = '"
        ,smartctlCheckResult
        ,"'"
        ,",`insertDateTime` = '"
        ,envDateTime
        ,"'"
    ]))
cs

cron 에 1분마다 동작하게 등록하면



요래됨 ㅎㅎ

'IT > python' 카테고리의 다른 글

python 서버상태 모니터링 세트  (0) 2015.07.22
Posted by xanasia

블로그 이미지
xanasia

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함