python 解析top文件格式

 1 top - 16:14:35 up 2 days,  3:04,  7 users,  load average: 2.22, 1.84, 1.77
 2 Tasks: 512 total,   2 running, 509 sleeping,   0 stopped,   1 zombie
 3 %Cpu0  :  5.0 us,  1.0 sy, 17.0 ni, 77.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 4 %Cpu1  :  5.9 us,  1.0 sy,  1.0 ni, 92.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 5 %Cpu2  :  7.0 us,  0.0 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 6 %Cpu3  :  4.0 us,  0.0 sy,  3.0 ni, 93.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 7 %Cpu4  :  5.8 us,  1.0 sy,  1.9 ni, 91.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 8 %Cpu5  : 22.0 us,  1.0 sy,  0.0 ni, 77.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 9 %Cpu6  :  4.0 us,  0.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
10 %Cpu7  :  4.0 us,  0.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
11 %Cpu8  :  4.0 us,  0.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
12 %Cpu9  : 18.6 us,  1.0 sy,  1.0 ni, 79.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
13 %Cpu10 :  3.9 us,  0.0 sy,  0.0 ni, 96.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
14 %Cpu11 :  3.9 us,  0.0 sy,  0.0 ni, 96.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
15 MiB Mem:  32067.54+total, 7194.383 used, 24873.16+free,  750.664 buffers
16 MiB Swap: 15258.99+total,    0.000 used, 15258.99+free. 1660.316 cached Mem
17 
18    PID    VIRT    RES    SHR S  %CPU %MEM COMMAND          nTH  P   SWAP   CODE    DATA nMaj nDRT   USED
19 148266 1786.7m 262.3m  65.6m S  46.3  0.8 compiz            17  3   0.0m   0.0m  995.3m  266    0 262.3m
20 145203  228.5m  70.3m  40.8m S  25.6  0.2 Xvnc               1  2   0.0m   4.3m   29.7m    0    0  70.3m
21   9024   32.1m   4.1m   3.3m S  21.6  0.0 fiberlamp          1  9   0.0m   0.0m    1.0m    0    0   4.1m
22   3535  214.9m  68.5m  45.2m S  20.7  0.2 Xvnc               1  4   0.0m   4.3m   23.6m   25    0  68.5m
23   7905   31.4m   3.4m   3.1m S   3.9  0.0 fuzzyflakes        1  5   0.0m   0.0m    0.4m    0    0   3.4m
24 145581   20.5m   3.0m   2.3m R   2.0  0.0 top                1  1   0.0m   0.1m    1.3m    1    0   3.0m
25   1454 12.947g 1.416g  31.9m S   1.0  4.5 java              51  0   0.0m   0.0m 12.809g  168    0 1.416g
26   3556  751.9m  66.7m  51.8m S   1.0  0.2 xfdesktop          3 11   0.0m   0.3m  300.4m  277    0  66.7m
27   8956   20.5m   2.9m   2.2m R   1.0  0.0 top                1  0   0.0m   0.1m    1.3m    0    0   2.9m

解析代码如下:

  1 # -*- coding: utf-8 -*-
  2 
  3 import sqlite3
  4 import os
  5 import time
  6 
  7 def create_load_info_table(cursor):
  8     create_sql = '''
  9         CREATE TABLE IF NOT EXISTS load_info(
 10         min1_load            REAL,
 11         min5_load            REAL,
 12         min15_load            REAL,
 13         record_time            TEXT,
 14         time_stamp            TEXT
 15         )
 16     '''
 17     cursor.execute(create_sql)
 18     
 19 def create_task_info_table(cursor):
 20     create_sql = '''
 21         CREATE TABLE IF NOT EXISTS task_info(
 22         total                INTEGER,
 23         running                INTEGER,
 24         sleeping            INTEGER,
 25         stopped                INTEGER,
 26         zombie                INTEGER,
 27         record_time            TEXT,
 28         time_stamp            TEXT
 29         )
 30     '''
 31     cursor.execute(create_sql)
 32     
 33 def create_cpu_info_table(cursor):
 34     create_sql = '''
 35         CREATE TABLE IF NOT EXISTS cpu_info(
 36         cpu_name            TEXT,
 37         us                    REAL,
 38         sy                    REAL,
 39         ni                    REAL,
 40         id                    REAL,
 41         wa                    REAL,
 42         hi                    REAL,
 43         si                    REAL,
 44         st                    REAL,
 45         record_time            TEXT,
 46         time_stamp            TEXT
 47         )
 48     '''
 49     cursor.execute(create_sql)
 50     
 51 def create_mem_info_table(cursor):
 52     create_sql = '''
 53         CREATE TABLE IF NOT EXISTS mem_info(
 54         total                REAL,
 55         used                REAL,
 56         free                REAL,
 57         buffers                REAL,
 58         record_time            TEXT,
 59         time_stamp            TEXT
 60         )
 61     '''
 62     cursor.execute(create_sql)
 63     
 64 def create_swap_info_table(cursor):
 65     create_sql = '''
 66         CREATE TABLE IF NOT EXISTS swap_info(
 67         total                REAL,
 68         used                REAL,
 69         free                REAL,
 70         cached                REAL,
 71         record_time            TEXT,
 72         time_stamp            TEXT
 73         )
 74     '''
 75     cursor.execute(create_sql)
 76     
 77 def create_process_info_table(cursor):
 78     create_sql = '''
 79         CREATE TABLE IF NOT EXISTS process_info(
 80         PID                    INTEGER,
 81         VIRT                REAL,
 82         RES                    REAL,
 83         SHR                    REAL,
 84         S                    TEXT,
 85         CPU                    REAL,
 86         MEM                    REAL,
 87         COMMAND                TEXT,
 88         nTH                    INTEGER,
 89         P                    INTEGER,
 90         SWAP                REAL,
 91         CODE                REAL,
 92         DATA                REAL,
 93         nMaj                INTEGER,
 94         nDRT                INTEGER,
 95         USED                REAL,
 96         record_time            TEXT,
 97         time_stamp            TEXT
 98         )
 99     '''
100     cursor.execute(create_sql)
101 
102 cur_dir = os.getcwd()
103 db_name = 'top_info.db'
104 top_log_name = 'top_cpu.txt'
105     
106 conn = sqlite3.connect(db_name)
107 cursor = conn.cursor()
108 get_all_table = "SELECT tbl_name FROM sqlite_master where type = 'table'"
109 cursor.execute(get_all_table)
110 all_table_list = cursor.fetchall()
111 all_table_name_list = []
112 
113 for table_name in all_table_list:
114     all_table_name_list.append(table_name[0])
115     delete_sql = "DELETE FROM %s" % (table_name[0])    #删除原来的记录
116     cursor.execute(delete_sql)
117     
118 cursor.execute("VACUUM")    # VACUUM 命令清除未使用的空间
119 conn.commit()
120 
121 topfile = open(top_log_name, 'r')
122 
123 try:
124     lines = topfile.readlines()
125     cur_time_stamp = ''
126     cur_record_time = ''
127     
128     for line in lines:
129         line = line.strip()
130         cur_time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
131         
132         if len(line) == 0:
133             continue
134         elif line[:3] == 'top':
135             if 'load_info' not in all_table_name_list:
136                 create_load_info_table(cursor)
137             
138             #['top - 08:14:17 up 2 days', ' 15:36', ' 15 users', '    load average: 5.46', ' 5.29', ' 5.18']
139             load_list = line.split(',')
140             #['top', '-', '08:14:17', 'up', '2', 'days']
141             cur_time_list = load_list[0].split()
142             cur_time = cur_time_list[2]
143             cur_record_time = cur_time
144             #['load average', ' 5.46']
145             min1_load_list = load_list[3].strip().split(':')
146             min1_load = float(min1_load_list[1])
147             min5_load = float(load_list[4])
148             min15_load = float(load_list[5])
149             
150             insert_sql = "INSERT INTO load_info VALUES (%0.2f, %0.2f, %0.2f, '%s', '%s')" % (min1_load, min5_load, min15_load, cur_record_time, cur_time_stamp)
151             cursor.execute(insert_sql)
152             #conn.commit()
153         elif line[:5] == 'Tasks':#Threads
154             if 'task_info' not in all_table_name_list:
155                 create_task_info_table(cursor)
156             
157             #'Tasks: 898 total,      5 running, 890 sleeping,     0 stopped,      3 zombie'
158             task_list = line.split(',')
159             #['Tasks: 898 total', '      5 running', ' 890 sleeping', '   0 stopped', '   3 zombie']
160             sum_task_count = int(task_list[0].strip().split(':')[1].split()[0])
161             running_task_count = int(task_list[1].strip().split()[0])
162             sleeping_task_count = int(task_list[2].strip().split()[0])
163             stopped_task_count = int(task_list[3].strip().split()[0])
164             zombia_task_count = int(task_list[4].strip().split()[0])
165             
166             insert_sql = "INSERT INTO task_info VALUES (%d, %d, %d, %d, %d, '%s', '%s')" % (sum_task_count, running_task_count, sleeping_task_count, stopped_task_count, zombia_task_count, cur_record_time, cur_time_stamp)
167             cursor.execute(insert_sql)
168             #conn.commit()
169         elif line[:4] == '%Cpu':#%Cpu0
170             if 'cpu_info' not in all_table_name_list:
171                 create_cpu_info_table(cursor)
172             
173             cpu_name = line[:line.index(':')].strip()
174             
175             #'%Cpu0     : 19.6 us,     2.0 sy,  5.8 ni, 72.6 id,    0.0 wa,     0.0 hi,  0.0 si,  0.0 st'
176             cpu_list = line.split(':')[1].strip().split(',')
177             #['19.6 us', '    2.0 sy', '    5.8 ni', ' 72.6 id', '    0.0 wa', '    0.0 hi', '    0.0 si', '    0.0 st']
178             us_percent = float(cpu_list[0].strip().split()[0])
179             #time running un-niced user processes
180             sy_percent = float(cpu_list[1].strip().split()[0])
181             #time running kernel processes
182             ni_percent = float(cpu_list[2].strip().split()[0])
183             #time running niced user processes
184             id_percent = float(cpu_list[3].strip().split()[0])
185             #time spent in the kernel idle handler
186             wa_percent = float(cpu_list[4].strip().split()[0])
187             #time waiting for I/O completion
188             hi_percent = float(cpu_list[5].strip().split()[0])
189             #time spent serving hardware interrupts
190             si_percent = float(cpu_list[6].strip().split()[0])
191             #time spent serving software interrupts
192             st_percent = float(cpu_list[7].strip().split()[0])
193             #time stolen from this vm by the hypervisor
194             
195             insert_sql = "INSERT INTO cpu_info VALUES ('%s', %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f, '%s', '%s')" % (cpu_name, us_percent, sy_percent, ni_percent, id_percent, wa_percent, hi_percent, si_percent, st_percent, cur_record_time, cur_time_stamp)
196             cursor.execute(insert_sql)
197             #conn.commit()
198         elif line[:7] == 'KiB Mem':#KiB Mem,主要用的交互选项E设置的单位
199             if 'mem_info' not in all_table_name_list:
200                 create_mem_info_table(cursor)
201             
202             #'KiB Mem:    32837164 total, 10604512 used, 22232652 free,  1117824 buffers'
203             Men_list = line.split(':')[1].strip().split(',')
204             #['32837164 total', ' 10604512 used', ' 22232652 free', '  1117824 buffers']
205             total_mem = float(Men_list[0].strip().split()[0]) / 1024
206             used_men = float(Men_list[1].strip().split()[0]) / 1024
207             free_men = float(Men_list[2].strip().split()[0]) / 1024
208             buffer_men = float(Men_list[3].strip().split()[0]) / 1024
209             
210             insert_sql = "INSERT INTO mem_info VALUES (%0.2f, %0.2f, %0.2f, %0.2f, '%s', '%s')" % (total_mem, used_men, free_men, buffer_men, cur_record_time, cur_time_stamp)
211             cursor.execute(insert_sql)
212             #conn.commit()
213         elif line[:7] == 'MiB Mem':#MiB Mem
214             if 'mem_info' not in all_table_name_list:
215                 create_mem_info_table(cursor)
216             
217             #MiB Mem:  32067.54+total, 5090.746 used, 26976.79+free,  624.168 buffers
218             Men_list = line.split(':')[1].strip().split(',')
219             
220             total_mem = 0.0
221             if '+' in Men_list[0]:
222                 total_mem = float(Men_list[0].strip().split('+')[0])
223             else:
224                 total_mem = float(Men_list[0].strip().split()[0])
225             
226             used_men = 0.0
227             if '+' in Men_list[1]:
228                 used_men = float(Men_list[1].strip().split('+')[0])
229             else:
230                 used_men = float(Men_list[1].strip().split()[0])
231                 
232             free_men = 0.0
233             if '+' in Men_list[2]:
234                 free_men = float(Men_list[2].strip().split('+')[0])
235             else:
236                 free_men = float(Men_list[2].strip().split()[0])
237                 
238             buffer_men = 0.0
239             if '+' in Men_list[3]:
240                 buffer_men = float(Men_list[3].strip().split('+')[0])
241             else:
242                 buffer_men = float(Men_list[3].strip().split()[0])
243                 
244             insert_sql = "INSERT INTO mem_info VALUES (%0.2f, %0.2f, %0.2f, %0.2f, '%s', '%s')" % (total_mem, used_men, free_men, buffer_men, cur_record_time, cur_time_stamp)
245             cursor.execute(insert_sql)
246             #conn.commit()
247         elif line[:8] == 'KiB Swap':    #主要用的交互选项E设置的单位
248             if 'swap_info' not in all_table_name_list:
249                 create_swap_info_table(cursor)
250             
251             #'KiB Swap: 15625212 total,           0 used, 15625212 free.  3900452 cached Mem'
252             Swap_list = line.split(':')[1].strip().split(',')
253             #['15625212 total', '         0 used', ' 15625212 free.    3900452 cached Mem']
254             total_swap = float(Swap_list[0].strip().split()[0]) / 1024
255             used_swap = float(Swap_list[1].strip().split()[0]) / 1024
256             
257             free_cache_list = Swap_list[2].strip().split('.')
258             
259             free_swap = float(free_cache_list[0].strip().split()[0]) / 1024
260             cache_swap = float(free_cache_list[1].strip().split()[0]) / 1024
261             
262             insert_sql = "INSERT INTO swap_info VALUES (%0.2f, %0.2f, %0.2f, %0.2f, '%s', '%s')" % (total_swap, used_swap, free_swap, cache_swap, cur_record_time, cur_time_stamp)
263             cursor.execute(insert_sql)
264             #conn.commit()
265         elif line[:8] == 'MiB Swap':
266             if 'swap_info' not in all_table_name_list:
267                 create_swap_info_table(cursor)
268             
269             #MiB Swap: 15258.99+total,    0.000 used, 15258.99+free. 1475.379 cached Mem
270             Swap_list = line.split(':')[1].strip().split(',')
271             
272             total_swap = 0.0
273             if '+' in Swap_list[0]:
274                 total_swap = float(Swap_list[0].strip().split('+')[0])
275             else:
276                 total_swap = float(Swap_list[0].strip().split()[0])
277                 
278             used_swap = 0.0
279             if '+' in Swap_list[1]:
280                 used_swap = float(Swap_list[1].strip().split('+')[0])
281             else:
282                 used_swap = float(Swap_list[1].strip().split()[0])
283                 
284             free_cache_list = Swap_list[2].strip().split('.')
285 
286             free_swap = 0.0
287             if '+' in free_cache_list[0]:
288                 free_swap = float(free_cache_list[0].strip().split('+')[0])
289             else:
290                 free_swap = float(free_cache_list[0].strip().split()[0])
291                 
292             cache_swap = 0.0
293             if '+' in free_cache_list[1]:
294                 cache_swap = float(free_cache_list[1].strip().split('+')[0])
295             else:
296                 cache_swap = float(free_cache_list[1].strip().split()[0])
297                 
298             insert_sql = "INSERT INTO swap_info VALUES (%0.2f, %0.2f, %0.2f, %0.2f, '%s', '%s')" % (total_swap, used_swap, free_swap, cache_swap, cur_record_time, cur_time_stamp)
299             cursor.execute(insert_sql)
300             #conn.commit()
301         elif line[:3] == 'PID':
302             continue
303         else:
304             if 'process_info' not in all_table_name_list:
305                 create_process_info_table(cursor)
306             
307             #PID    VIRT    RES       SHR S  %CPU %MEM COMMAND       nTH    P    SWAP   CODE       DATA nMaj nDRT    USED
308             #'157271  459.9m 256.8m     13.6m R  92.3    0.8 bundle         2    0    0.0m   0.0m     251.6m       0    0 256.8m'
309             process_list = line.split()
310             PID = int(process_list[0])
311             #Process Id
312             
313             VIRT = 0.0
314             if 'm' in process_list[1]:    #这里的显示单位交互模式下用的是小写的e作为设置
315                 VIRT = float(process_list[1][:-1])
316             elif 'g' in process_list[1]:
317                 VIRT = float(process_list[1][:-1]) * 1024
318             else:
319                 VIRT = float(process_list[1]) / 1024
320             #Virtual Memory Size
321             #The total amount of virtual memory used by the task. It includes all code, data and shared libraries
322             #plus pages that have been swapped out and pages that have been mapped but not used.
323             
324             RES = 0.0
325             if 'm' in process_list[2]:
326                 RES = float(process_list[2][:-1])
327             elif 'g' in process_list[2]:
328                 RES = float(process_list[2][:-1]) * 1024
329             else:
330                 RES = float(process_list[2]) / 1024
331             
332             #Resident Memory Size
333             #A subset of the virtual address space(VIRT) representing the non-swapped physical memory a task is 
334             #currently using.
335             
336             SHR = 0.0
337             if 'm' in process_list[3]:
338                 SHR = float(process_list[3][:-1])
339             elif 'g' in process_list[3]:
340                 SHR = float(process_list[3][:-1]) * 1024
341             else:
342                 SHR = float(process_list[3]) / 1024
343             
344             #Shared Memory Size
345             #A subset of resident memory(RES) that may be used by other processes.
346             process_status = process_list[4]
347             #D = uninterruptible sleeping
348             #R = running
349             #S = sleeping
350             #T = stopped by job control signal
351             #t = stopped by debugger during trace
352             #Z = zombia
353             cpu_usage = float(process_list[5])    #百分比对应的是单个cpu还是cpu之和使用的是交互选项I设置的
354             mem_usage = float(process_list[6])
355             command_name = process_list[7]
356             thread_count = int(process_list[8])
357             use_cpu_index = int(process_list[9])
358             
359             SWAP = 0.0
360             if 'm' in process_list[10]:
361                 SWAP = float(process_list[10][:-1])
362             elif 'g' in process_list[10]:
363                 SWAP = float(process_list[10][:-1]) * 1024
364             else:
365                 SWAP = float(process_list[10]) / 1024
366             
367             #The formerly resident portion of a task's address space written to the swap file when physical memory
368             #becomes over committed.
369             
370             CODE = 0.0
371             if 'm' in process_list[11]:
372                 CODE = float(process_list[11][:-1])
373             elif 'g' in process_list[11]:
374                 CODE = float(process_list[11][:-1]) * 1024
375             else:
376                 CODE = float(process_list[11]) / 1024
377             #Code Size
378             #The amount of physical memory currently devoted to executable code, also know as the Text Resident Set or TRS
379             
380             DATA = 0.0
381             if 'm' in process_list[12]:
382                 DATA = float(process_list[12][:-1])
383             elif 'g' in process_list[12]:
384                 DATA = float(process_list[12][:-1]) * 1024
385             else:
386                 DATA = float(process_list[12]) / 1024
387             
388             #Data + Stack Size
389             #The amount of private memory reserved by a process. It is also known as the Data Resident Set or DRS.
390             #Such memory may not yet be mapped to physical memory(RES) but will always be included in the 
391             #virtual memory (VIRT) amount.
392             nMaj = int(process_list[13])
393             #Major Page Fault Count
394             #The number of major page faults that have occurred for a task. A page fault occurs when a process attempts
395             #to read from or write to a virtual page that is not currently present in its address.
396             #A major page fault is when auxiliary storage access is invloved in making that page available.
397             nDRT = int(process_list[14])
398             #Dirty Pages Count
399             #The number of pages that have been modified since they were last written to auxiliary storage. Dirty pages
400             #must be written to auxiliary storage before the corresponding physical memory location can be used for
401             #some other virtual page.
402             
403             USED = 0.0
404             if 'm' in process_list[15]:
405                 USED = float(process_list[15][:-1])
406             elif 'g' in process_list[15]:
407                 USED = float(process_list[15][:-1]) * 1024
408             else:
409                 USED = float(process_list[15]) / 1024
410             
411             #Memory in Use
412             #This field represents the non-swapped physical memory a task is using (RES) plus the swapped out portion
413             #of its address space (SWAP).
414             
415             #8943  397.8m 218.4m  12.7m R  95.9  0.7 bundle             2  9   0.0m   0.0m  216.3m    0    0 218.4m
416             insert_sql = "INSERT INTO process_info VALUES (%d, %0.2f, %0.2f, %0.2f, '%s', %0.2f, %0.2f, '%s', %d, %d, %0.2f, %0.2f, %0.2f, %d, %d, %0.2f, '%s', '%s')" % (PID, VIRT, RES, SHR, process_status, cpu_usage, mem_usage, command_name, thread_count, use_cpu_index, SWAP, CODE, DATA, nMaj, nDRT, USED, cur_record_time, cur_time_stamp)
417             cursor.execute(insert_sql)
418             #conn.commit()
419 finally:
420     topfile.close()
421     cursor.close()
422     conn.commit()
423     conn.close()

 

posted on 2017-11-12 16:37  帅胡  阅读(1699)  评论(0编辑  收藏  举报

导航