本文主要是介绍资源监控_自己编写一个nmon,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
监控资源是测试工作中非常常见的事情,悲催的是每年都会临到用时又折腾一下。因为操作系统不断的在改变,监控的工具也在不断的随着改变。要么去搞搞rpc,然后用loadrunner连接上系统来监控,要么就搞个类似nmon一类的工具来监控,生成漂亮的图表。等到多年以后,发现其实真正需要监控的参数就那么几个,cpu、mem、disk和net,再多了未必用得到,还消耗资源,比如监控磁盘读写队列,有用吗?没用吗?要是disk增长很迅速,cpu占用大,大多数人都会意识到磁盘读写的很厉害了,所以监控磁盘读写队列未必那么有用,够用就好了。使用nmon后,觉得挺省心的,不过就有个不爽的地方,对系统是监控了一大堆,但是对进程没监控。
基于上面的两个需求,一是监控足够少的参数,二是对进程要有监控,所以搞了一个我的nmon。说起来很简单,就是以脚本生成一大堆的csv,然后使用一个vba脚本汇总生成excel文件,同时生成图表。
好了,来介绍一下mynmon吧,在ubuntu下测试通过,vba脚本在office2007下测试通过。不过我还是没太足的信心,欢迎大家帮忙测试一下。
脚本:mynmon.sh:
脚本包括两部分: 一部分是对系统的CPU,NET,MEM,DISK进行的监控 另一部分是对参数中的进程列表进行的监控,细节好像也没啥好说的了,大家自己看吧
另:调试信息的是顶头两个#的行,需要开启调试信息的时候,就直接vi文件,使用 :%s/^##// 替换掉,要关闭调试信息,就把顶头的echo加上两个# |
#Two parts: system & process #usage:./mynmon.sh interval "processname1 processname2 processname3..."
SLEEP_TIME=$1 P_LIST=$2
OUT1="SYS.CSV" OUT2TMP=".CSV" OUT2="" S1="" STMP1="" S2="" STMP2="" PID=""
rm *.CSV
#system resource S1="NETIN(k),NETOUT(k),DISK USED(k),CPU 1MIN LOAD,MEM FREE(k)\n" printf "$S1">$OUT1
while : do
#1.network STMP1=`/sbin/ifconfig eth0|sed -n "8"p|awk '{print $2}'|awk -F: '{print $2}'` ##echo "NET IN:"$STMP1 STMP1=`echo $STMP1"/1000"|bc` S1="$STMP1," STMP1=`/sbin/ifconfig eth0|sed -n "8"p|awk '{print $6}'|awk -F: '{print $2}'` ##echo "NET OUT:"$STMP1 STMP1=`echo "$STMP1/1000"|bc` S1="$S1$STMP1,"
#2.disk STMP1=`df --total|grep total|awk '{print $3}'` ##echo "DISK:"$STMP1 S1="$S1$STMP1,"
#3.cpu one minute load
STMP1=`uptime|awk -F, '{print $4}'|awk '{print $3}'` ##echo "CPU:"$STMP1 S1="$S1$STMP1,"
#4.mem free
STMP1=`vmstat 1 1|sed -n '3p'|awk '{print $4}'` ##echo "MEM:"$STMP1 S1="$S1$STMP1\n"
##echo "$S1" printf "$S1">>$OUT1
#process resource for p in $P_LIST do OUT2="$p""_""$OUT2TMP" ##echo "PROCESS:$p OUT2:$OUT2" if [ -f "$OUT2" ] then : else printf "CPU LOAD,MEM(m)\n">"$OUT2" fi
#1.get pid PID=`ps -ef|grep $p|grep -v grep|grep -v mynmon|awk '{print $2}'|sed -n '1p'`
if [ "-$PID" = "-" ] then { echo "The process <$p> does not exist." echo "The process <$p> does not exist." >$OUT2 } else { #1.CPU STMP2=`ps -p $PID -o pcpu |grep -v CPU | awk '{print $1}'` ##echo "PROCESS $p CPU:"$STMP2 S2="$STMP2,"
#2.MEM STMP2=`ps -o rss -p $PID|grep -v RSS` STMP2=`echo "$STMP2/1000"|bc` ##echo "PROCESS $p MEM:$STMP2" S2="$S2$STMP2"
##echo "$S2" printf "$S2\n">>"$OUT2" } fi
done
sleep $SLEEP_TIME done
|
启动方式: nohup ./mynmon.sh 120 “key1 key2 key3 key4” |
处理脚本:mynmonlog.xls:
使用方法:
001.jpg |
算法: 1.新建excel文件; 2.寻找需要处理目录下的所有csv文件,根据<目录名_文件名>,给excel添加sheet,顺带删除了缺省的sheet1,sheet2和sheet3; 3.打开csv文件,把内容粘贴进sheet中; 4.SYS文件单独生成:(netin,netout和net),cpu,mem free和chart; 5.WIN文件单独生成:(netin,netout和net),cpu,mem free和chart; 6.其他文件生成cpu和mem free的chart; 7.保存为配置的结果文件
|
Private Declare Function OpenClipboard Lib "user32" (ByVal Hwnd As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long Private Declare Function EmptyClipboard Lib "user32" () As Long
Sub EraseClipboard() If (OpenClipboard(0&)) Then Call EmptyClipboard Call CloseClipboard End If End Sub
Sub Main()
Dim objExcel As Excel.Application Dim Excelbook As Workbook Dim Excelsheet As Worksheet
Dim strXls
Dim objExcel1 As Excel.Application Dim Csvbook As Workbook Dim Csvsheet As Worksheet
Dim fso, f, f1, fc, myFile Dim strDoFolder, strSubFolder, strDoFile
Dim lngCnt, lngI Dim g As ChartObject
Dim dt1, dt2 dt1 = Now
strDoFolder = ThisWorkbook.Path & "\" & ThisWorkbook.ActiveSheet.Cells(3, 2).Value & "\" strXls = ThisWorkbook.Path & "\" & ThisWorkbook.ActiveSheet.Cells(4, 2).Value 'Debug.Print strDoFolder 'Debug.Print strXls
Set objExcel = CreateObject("Excel.Application") Set Excelbook = objExcel.Workbooks.Add '新建EXCEL文件 objExcel.Visible = True
Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(strXls) Then fso.DeleteFile strXls, True End If
Set f = fso.GetFolder(strDoFolder) Set fc = f.SubFolders For Each f1 In fc strSubFolder = strDoFolder & f1.Name If f1.Files.Count > 0 Then For Each myFile In f1.Files strDoFile = strSubFolder & "\" & myFile.Name 'DEBUG.PRINT UCase(fso.GetExtensionName(strDoFile)) & vbCrLf & strDoFile
If UCase(fso.GetExtensionName(strDoFile)) = "CSV" Then Set Excelsheet = Excelbook.Worksheets.Add '添加工作页 Excelsheet.Name = Left(f1.Name & "_" & fso.GetBaseName(strDoFile), 30)
Set objExcel1 = CreateObject("Excel.Application") Set Csvbook = objExcel1.Workbooks.Open(strDoFile) '打开CSV文件 Set Csvsheet = Csvbook.Worksheets(1) '取CSV文件的sheet
objExcel1.Visible = True
Csvsheet.Activate Csvsheet.Cells.Select objExcel1.Selection.Copy
Excelsheet.Activate Excelsheet.Cells(1, 1).Select Excelsheet.Paste
'对于不同的SYS,WIN,其他进程的监控结果不同的处理 If UCase(Left(fso.GetBaseName(strDoFile), 3)) = "SYS" Then
lngCnt = Excelsheet.UsedRange.Rows.Count Excelsheet.Cells(1, 6).Value = "NETIN(K)" Excelsheet.Cells(1, 7).Value = "NETOUT(K)" Excelsheet.Cells(1, 8).Value = "NET(K)"
For lngI = 2 To lngCnt - 1 Excelsheet.Cells(lngI, 6).Value = Excelsheet.Cells(lngI + 1, 1).Value - Excelsheet.Cells(lngI, 1).Value Excelsheet.Cells(lngI, 7).Value = Excelsheet.Cells(lngI + 1, 2).Value - Excelsheet.Cells(lngI, 2).Value Excelsheet.Cells(lngI, 8).Value = Excelsheet.Cells(lngI, 6).Value + Excelsheet.Cells(lngI, 7).Value Next
Set g = Excelsheet.ChartObjects.Add(400, 10, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$F:$H") End With
Set g = Excelsheet.ChartObjects.Add(450, 60, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$D:$D") End With
Set g = Excelsheet.ChartObjects.Add(500, 110, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$E:$E") End With
ElseIf UCase(Left(fso.GetBaseName(strDoFile), 3)) = "WIN" Then
Set g = Excelsheet.ChartObjects.Add(450, 60, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$B:$C") End With
Set g = Excelsheet.ChartObjects.Add(500, 110, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$G:$G") End With
Else
Set g = Excelsheet.ChartObjects.Add(450, 60, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$A:$A") End With
Set g = Excelsheet.ChartObjects.Add(500, 110, 400, 250) With g.Chart .ChartType = xlLine .SetSourceData Source:=Excelsheet.Range("$B:$B") End With
End If
'清空剪切板 Call EraseClipboard
Csvbook.Close SaveChanges:=False
Set Csvsheet = Nothing Set Csvbook = Nothing Set objExcel1 = Nothing
Else
End If Next End If
Next
Excelbook.Worksheets("Sheet1").Delete Excelbook.Worksheets("Sheet2").Delete Excelbook.Worksheets("Sheet3").Delete Excelbook.SaveAs strXls Excelbook.Close
Set Excelsheet = Nothing Set Excelbook = Nothing Set objExcel = Nothing
Set fso = Nothing Set f = Nothing Set fc = Nothing
dt2 = Now MsgBox "执行完成,总共用时:" & DateDiff("s", dt1, dt2) & "秒"
End Sub |
上面的内容牵涉到了windows的资源监控,我比较BT,不喜欢用loadrunner的监控,喜欢自己开启计数器,方法不用说了吧,生成csv文件的命令如下:
relog 001.blg -f CSV -o WINNMON.csv |
这里附带说起另外一个话题,如果只是监控一台服务器,这事容易,如果要监控很多台呢,光是把mynmon.sh放上去,启动起来,估计都能把你搞崩溃了。如何自动上传文件,自动执行,自动停止脚本,自动取下结果,这是另外一件事情,有兴趣的同学可以搜搜winscp就会有法子了。
附件:
http://download.csdn.net/detail/testingba/4427796
这篇关于资源监控_自己编写一个nmon的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!