Python plotting cpu/mem monitoring curve

Source: Internet
Author: User

1
Example of input log format: [12:55:31] 16070 sosotest 20 0 8302 m 41 m 11 m S 0 0.1. 93 java 42836 8501676 HandleNum: 28
View sourceprint?
001
#-*-Coding: cp936 -*-
002
Import re, sys, OS
003
Import time
004
From pylab import *
005
'''
006
Modify: 2010.04.23 Add the start time information to modify the parameter, and use the process number as part of the file name
007
Modify: 2010.04.25 call 1 and 2 parameters. The first parameter is the log file, and the second parameter is the process id.
008
Modify: the core point is added to 2010.10.31. The core is recorded in the program running directory.
009
Modify: 2010.11.30 draw the two graphs together, and add the log to the memory details generated by ps.
010
Modify: 2011.06.24 add process name, indicating valid result log
011
'''
012
 
013
Def getCoreLst (proclst ):
014
RetLst = []
015
CurPid = proclst [0]
016
For I in range (1, len (proclst )):
017
If curPid! = Proclst [I]:
018
CurPid = proclst [I]
019
RetLst. append (I + 1)
020
Return retLst
021

022
Virt = []
023
Res = []
024
Cpu = []
025
Proc = []
026
Allrcd = [] # All records
027
TimeInterval = 10
028
Processname = "empty process"
029
If len (sys. argv )! = 5 and len (sys. argv )! = 4:
030
Print "Usage: python % s fileName mem_cpu.png processname timeinterval [default: 10]" % (_ file __)
031
Print "Usage: python % s fileName processname timeinterval [default: 10]" % (_ file __)
032
Sys. exit ()
033
 
034
Print "virt res cpu %"
035
If len (sys. argv) = 5:
036
SrcfileName = sys. argv [2]
037
Processname = sys. argv [3]
038
TimeInterval = int (sys. argv [4])
039
Else:
040
SrcfileName = "cpu_mem.png"
041
Processname = sys. argv [2]
042
TimeInterval = int (sys. argv [3])
043
 
044
BFoundBeginTime = False
045
BFoundEndTime = False
046
 
047
StrCurTime = ""
048
StrBeginTime = ""
049
StrEndTime = ""
050
 
051
 
052
F = open (sys. argv [1], "r ")
053
Line = f. readline ()
054
StrBeginTime = line [line. find ('[') + 1: line. find (']')]
055
F. seek (0, OS. SEEK_SET)
056
While True:
057
Line = f. readline ()
058
If len (line) = 0: break
059
# If line. find ("ProcessID: % s" % processId) =-1:
060
# Continue
061
Line = line. strip ()
062
 
063
StrEndTime = line [line. find ('[') + 1: line. find (']')] # Get the current time
064
 
065
Try:
066
If len (line. split ())! = 17:
067
Continue
068
If not processname in line:
069
Continue
070
Lp = line. split () [2:]
071
Lp0 = []
072
For I in range (0, len (lp )):
073
If len (lp [I])> 0: l1_append (lp [I])
074
 
075
# Print lp0 [] process number
076
Proc. append (int (lp0 [0])
077

078
# Virtual memory size
079
If lp0 [4] [-1]! = 'M' and lp0 [4] [-1]! = 'G': fRet = float (lp0 [4])/(1024*1024)
080
If lp0 [4] [-1] == 'M': fRet = float (lp0 [4] [0:-1])/1024
081
If lp0 [4] [-1] = 'G': fRet = float (lp0 [4] [0:-1])
082
Virt. append (fRet)
083

084
# Actual memory size
085
If lp0 [5] [-1]! = 'M' and lp0 [5] [-1]! = 'G': fRet = float (lp0 [5])/(1024*1024)
086
If lp0 [5] [-1] == 'M': fRet = float (lp0 [5] [0:-1])/1024
087
If lp0 [5] [-1] = 'G': fRet = float (lp0 [5] [0:-1])
088
Res. append (float (fRet ))
089

090
# Cpu usage
091
Cpu. append (float (lp0 [8])
092
Allrcd. append (line)
093
 
094
Except t:
095
Print "Line data Error as follow ..."
096
Print line
097
# Time. sleep (1)
098
# Print "abc", lp0 [4], lp0 [5], lp0 [8]
099
F. close ()
100
 
101
Print "Show len", len (virt)
102
Print "Time consumed %. 3f hours" % (timeInterval * len (virt)/(60*60.0 ))
103
Print "Start Time: % s" % strBeginTime
104
Print "End Time: % s" % strEndTime
105
 
106
Print virt
107
XSpan = len (virt)
108
A = max (virt)
109
B = max (res)
110
YSpan = max (a, B) # highest memory point
111
Cpu_mem_rate = max (cpu)/ySpan # Calculate the normalized ratio of cpu to memory
112
 
113
# Normalized CPU consumption
114
For I in range (0, len (cpu )):
115
Cpu [I] = cpu [I]/cpu_mem_rate
116
 
117
 
118
Corelst_x = getCoreLst (proc)
119
 
120
Corelst_cpu_y = []
121
Corelst_1__y = []
122
Corelst_res_y = []
123
 
124
For I in range (0, len (corelst_x )):
125
Corelst_cpu_y.append (cpu [corelst_x [I])
126
Corelst_virt_y.append (virt [corelst_x [I])
127
Corelst_res_y.append (res [corelst_x [I])
128
 
129
# Start of counting core points
130
FpCorePoint = file ("error_core.log", "")
131
For I in range (0, len (corelst_x )):
132
FpCorePoint. write ("% s \ n" % allrcd [corelst_x [I])
133
FpCorePoint. close ()
134
# End of statistics core point
135
 
136
 
137
# Multiply the x-axis coordinates by the time interval to draw points
138
For I in range (0, len (corelst_x )):
139
Corelst_x [I] = corelst_x [I] * timeInterval
140
 
141
 
142
StrDuration = "StartTime: % s EndTime: % s \ n Duration % d minutes % d hours" % (strBeginTime, strEndTime, len (virt) * timeInterval/60, len (virt) * timeInterval/3600)
143
X = []
144
For I in range (0, len (virt), 1 ):
145
X. append (timeInterval * I) # obtain information every 10 seconds
146
 
147
# Draw a CPU diagram first
148
Print "Cpu len: % d" % (len (x), len (cpu ))
149
Figure (1)
150
Axis ([0, len (cpu), 0, max (cpu)]) # layout the drawing Area
151
StrTitle = "______________________________ \ n" + strDuration + "CPU_rate: %. 2f \ n --------------------------" % cpu_mem_rate
152
Title (strTitle, fontsize = 12)
153
Xlabel ("timereceivsed/sec", fontsize = 12, ha = 'left ')
154
Ylabel ('cpu-Rate: %. 2f | MEM/G' % cpu_mem_rate, fontsize = 12, va = 'top ')
155
Plot (x, cpu, 'G') # plot cpu
156
# Drawing core points
157
Print "CPU Core Info :"
158
Print "Core at", corelst_x
159
Print "Core at", corelst_cpu_y
160
# Plot (corelst_x, corelst_cpu_y, "rs") # Draw cpu core points
161
 
162
# Title ("MEM-% s \ n % s" % (strBeginTime, strDuration), fontsize = 12)
163
# Xlabel ("timereceivsed/sec", fontsize = 12)
164
# Ylabel ('memery/G', fontsize = 12)
165
Plot (x, virt, 'B') # Draw virtual memory
166
Plot (x, res, 'R') # Draw Physical memory
167
# Drawing core points
168
Print "MEM Core Info :"
169
Print "Core at", corelst_x
170
Print "Core at", corelst_1__y
171
Print "Core at", corelst_res_y
172
 
173
# Plot (corelst_x, corelst_1__y, "rs") # Draw core points of virtual memory
174
Plot (corelst_x, corelst_res_y, "cs") # Draw core points of physical memory
175
 
176
Grid ()
177
Savefig (srcfileName, dpi = 85)
178
 
179
StrCmd = srcfileName + "/B"
180
OS. system (strCmd)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.