需求
在server程序運行到一半或者要結束的時候希望它自動記錄下當前server的狀態,包括有多少個進程,每個占多少CPU,多少內存,系統還剩多少內存之類的。
想法
很簡單,既然程序有python腳本,那么就通過python腳本開一個進程運行一些power shell的腳本,把結果寫文件。
PowerShell
花了一天時間,啃了點文檔,記錄一些例子:
- 獲取幫助:get-help
- get-help *
- get-help get-help -detailed
- 獲取object的member
- get-process | get-member
- 獲取進程列表然后輸出用CPU時間最多的5個和最少的5個,並把結果寫到 D:\a.txt
- get-process | sort-object CPU -descending | select-object -first 5 -last 5 | out-file D:\a.txt
- 輸出到csv或者xml
- get-process | Export-Csv D:\ccc.csv
- get-process | Export-Clixml d:\ccc.xml
- 輸出為HTML
- get-service | convertto-html -property name,status | out-file D:\ab.html
- 輸出帶顏色
- write-host "aaa" -ForegroundColor red -BackgroundColor blue
- 把當前的service按satus排序,輸出service的名字和satus,並且根據status的狀態用不同顏色輸出
- get-service | sort-object status | foreach-object{if ($_.status -eq "stopped"){write-host $_.Name $_.status -ForegroundColor red} elseif($_.status -eq "Running"){write-host $_.Name $_.status -ForegroundColor green}}
- 文件操作:獲取power shell的drive
- get-psdrive
- 文件操作:創建一個新的drive
- new-psdrive -name testps -psprovider filesystem -root d:\testps
- 列出所有.py文件
- get-childitem *.py
- Get-ChildItem | Where-Object {$_.extension -eq ".xml"}
- 文件操作:根據擴展名分組,並且按組里的文件數量排序輸出
- Get-ChildItem | group-object extension | sort-object Count -descending
- 文件操作: 統計文件夾里的所有xml文件,但是最終只輸出所有xml文件的大小之和
- Get-ChildItem *.xml | Measure-Object length -sum -average -maximum -minimum | foreach-object {write-host $_.sum}
- 刪除所有.py文件
- remove-item *.py
- 給文件夾里的每個文件按擴展名創建一個文件夾
- Get-ChildItem | sort-object extension -Unique | ForEach-Object {new-item $_.extension -type directory}
- 把文件夾里的文件都挪到上面創建的文件夾里去
- Get-ChildItem | Where-Object {$_.GetType() -notmatch "d"} | ForEach-Object {Move-Item $_ $_.Extension}
- 操作wmi object
- Get-WmiObject -class win32_computersystem
- (Get-WmiObject -class win32_computersystem).UserName
- Get-WmiObject -class win32_desktop -ComputerName keke-pc3
- 操作.NET的object,訪問一個rss地址並且打印內容
- ([xml](New-Object net.webclient).DownloadString( "http://blogs.msdn.com/powershell/rss.aspx" )).rss.channel.item | format-table title,link
- 操作COM object,創建並寫Excel文檔
- PS testps:\> $a = new-object -comobject excel.application
- PS testps:\> $b = $a.Workbooks.Add()
- PS testps:\> $c = $b.Worksheets.Item(1)
- PS testps:\> $c.Cells.Item(1,1) = "windows powershiell rocks"
- PS testps:\> $a.Visible = $True
- PS testps:\> $b.SaveAs(".\Test.xls")
- PS testps:\> $a.Quit()
- 操作COM object,調用IE訪問一個網站
- PS testps:\> $ie = new-object -comobject InternetExplorer.application
- PS testps:\> $ie.Visible = $True
- PS testps:\> $ie | get-member
- $ie.Navigate("http://www.baidu.com")
- 操作eventlog
- get-eventlog -list
- get-eventlog system -newest 3 | format-list
- 操作WMI獲取電腦信息
- 內存:Get-WmiObject -Class Win32_PhysicalMemory
- CPU:Get-WmiObject -Class Win32_Processor
- 進程:Get-WmiObject -Class Win32_PerfRawData_PerfProc_Process > D:\temp.txt
- 更多wmi的object:http://msdn.microsoft.com/en-us/library/aa394277%28VS.85%29.aspx
Python調用PowerShell
不想引入其他第三方庫,那么就開一個子進程,讓子進程自己運行powershell並且寫文件
關於python調用子進程的文檔在這里:
http://docs.python.org/2.7/library/subprocess.html?highlight=subprocess#subprocess
這里列一些要點:
- subprocess是用來取代os... popen2... commands...的
- subprocess.call會導致當前進程阻塞直到得到子進程返回值
- subprocess.check_call和subprocess.call一樣,區別在於如果子進程返回非0,那么會拋出CalledProcessError
- subprocess.check_output返回的不是返回值而是子進程的output,如果子進程返回值非0,也會拋出CalledProcessError。
- subprocess.PIPE: 給stdin, stdout, stderr指定一個管道
- subprocess.STDOUT:可以用在stderr上,讓子進程的錯誤也輸出在stdout流中
- subprocess.Popen是那個真正工作的類
- Popen.poll()
- Popen.wait()
- Popen.communicate(input=None) 把input給子進程,返回的是子進程的(stdoutdata, stderrdata)
- Popen.send_signal()
- Popen.terminate()
- Popen.kill()