提及自動化運維離不開監控軟件,那咱們就用一點時間來寫一個主機性能監控系統吧,Python+Django使用HighChart.js能夠很快實現性能監控的功能:html
如下爲監控視圖效果。本機總內存8G,內存佔用一直比較穩定在55%~58%之間,因此圖形曲線很是平緩~。python
內存監控使用Ajax和Python第三方插件以及HighChart.js生成監控視圖:linux
前臺Ajax和HighChart.js引用代碼以下:git
Highcharts.setOptions({ global: { useUTC: false } }); var totalmem = 0,usedmem = 0; $.ajax({ url:"/getMemInfo/", async:false, success:function(data){ var jsondata= JSON.parse(data); totalmem = jsondata.total_mem; usedmem = jsondata.used_mem; } }); var chart; $("#memusage-container").highcharts({ chart: { type: 'area', marginRight: 10, events: { load: function() { // set up the updating of the chart each second var series0 = this.series[0];// ,series1= this.series[1]; setInterval(function() { var x = (new Date()).getTime(), // current time y = Math.random(),system=Math.random(); $.ajax({ url:"/getMemInfo/", async:false, success:function(data){ var jsondata= JSON.parse(data); y = jsondata.used_mem; } }); series0.addPoint([x, y], true, true); // series1.addPoint([x,system],true,true); }, 15000); } } }, title: { text: "Live Memory Usage (Total Memory: " + totalmem+" MB)" }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: 'Memory Used Percentage(%)', }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.system, 2); } }, legend: { enabled:true }, backgroundColor: '#dddddd', plotOptions:{ area:{ // marker:{enabled:false}, // linecolor:'#000', color:'#f27d2f', // fillColor:'#ecae3d', fillOpacity:0.8, marker: { enabled: false, symbol: 'circle', radius: 2, states: { hover: { enabled: true } } } } }, exporting: { enabled: false }, series: [ { name: 'Memory Used Percentage', data: (function() { // generate an array of random data var data = [], time = (new Date()).getTime(), i; for (i = -5; i <= 0; i++) { data.push({ x: time + i * 1000, y: usedmem }); } return data; })() } ] });
後臺getMeminfo函數調用了psutil的virtual_meory()方法github
def getMemInfo(request): m = psutil.virtual_memory() total_mem = m.total / 1024 / 1024 used_mem_percentage = m.percent free_mem_percentage = 100 - m.percent print '{"total_mem":%s,"used_mem": %s,"free_mem":%s}' % (total_mem, used_mem_percentage, free_mem_percentage) return HttpResponse( '{"total_mem":%s,"used_mem": %s,"free_mem":%s}' % (total_mem, used_mem_percentage, free_mem_percentage))
實現效果以下圖:ajax
CPU性能視圖,使用Ajax和Python第三方插件以及HighChart.js生成監控視圖:json
前臺Ajax和HighChart.js引用代碼以下:app
var usrcpu =0,systemcpu =0; $.ajax({ url:"/getCPUInfo/", async:false, success:function(data){ var jsondata= JSON.parse(data); usrcpu = jsondata.user; systemcpu = jsondata.system; } }); $("#container").highcharts({ chart: { type: 'area', // animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function() { // set up the updating of the chart each second var series0 = this.series[0],series1= this.series[1]; setInterval(function() { var x = (new Date()).getTime(), // current time y = Math.random(),system=Math.random(); $.ajax({ url:"/getCPUInfo/", async:false, success:function(data){ var jsondata= JSON.parse(data); y = jsondata.user; system = jsondata.system; } }); // alert('x and y is :'+x+","+y); series0.addPoint([x, y], true, true); series1.addPoint([x,system],true,true); }, 15000); } } }, title: { text: "Live CPU and System Data(%)" }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: 'Value' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.system, 2); } }, legend: { enabled:true }, plotOptions:{ area:{ // fillColor:'#ecae3d', fillOpacity:0.8, marker: { enabled: false, symbol: 'circle', radius: 2, states: { hover: { enabled: true } } } } }, exporting: { enabled: false }, series: [{ name: 'User data', data: (function() { // generate an array of random data var data = [], time = (new Date()).getTime(), i; for (i =-19 ; i <= 0; i++) { data.push({ x: time + i * 1000, y: usrcpu //Math.random() }); } return data; })(), // color:'#f28f43' }, {name:'System data', data:(function(){ var data=[], time =(new Date()).getTime(), i; for(i=-19;i<=0;i++){ data.push({ x:time + i*1000, y:systemcpu//Math.random() }); } return data; })(), //color:'#492970' } ] });
後臺getCPUinfo函數使用的psutil插件的cpu_time_percent方法:運維
def getCPUInfo(request): a = psutil.cpu_times_percent() user_percentage = a.user system_percentage = a.system print "user and system are ", user_percentage, system_percentage return HttpResponse('{"user": %s,"system":%s}' % (user_percentage, system_percentage))
實現效果以下:CPU使用率監控視圖,CPU使用率波動頻繁,因此效果很好:dom
貼上任務管理器中的監控對比一下。
磁盤使用狀況監控視圖:這一部分首先是使用基於類的通用視圖獲取監控目標對象的分區信息,展示在頁面上,方便監控時進行切換查看。而後使用Jquery以及Ajax來調用後臺接口getDiskInfo獲取對應盤符的空間佔用狀況:
前臺Jquery與Ajax實現代碼:
var disk_u =0,disk_f =0,disk_name = ""; $(document).ready(function(){ $("span.disk_change").click(function(){ var disk_change = $(this).html(); console.log(disk_change) $.ajax({ url:"/getDiskInfo/"+disk_change, async:false, success:function(data){ console.log(data) var jsondata= JSON.parse(data); disk_u = jsondata.disk_used; disk_f = jsondata.disk_free; disk_name = jsondata.disk_name; } }); $(function () { $('#idget-id-server').highcharts({ chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false }, title: { text: '主機'+disk_name+'盤使用狀況監控視圖' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, color: '#000000', connectorColor: '#000000', format: '<b>{point.name}</b>: {point.percentage:.1f} %' } } }, series: [{ type: 'pie', name: 'Disk Usage', data: [ ['Used', disk_u], ['Free', disk_f], ] }] }); }); }); $.ajax({ url:"/getDiskInfo/{{default_disk}}", async:false, success:function(data){ console.log(data) var jsondata= JSON.parse(data); disk_u = jsondata.disk_used; disk_f = jsondata.disk_free; disk_name = jsondata.disk_name; } }); $(function () { $('#idget-id-server').highcharts({ chart: { plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false }, title: { text: '主機'+disk_name+'盤使用狀況監控視圖' }, tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotOptions: { pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, color: '#000000', connectorColor: '#000000', format: '<b>{point.name}</b>: {point.percentage:.1f} %' } } }, series: [{ type: 'pie', name: 'Disk Usage', data: [ ['Used', disk_u], ['Free', disk_f], ] }] }); }); });
後臺getDiskinfo接口實現代碼:
def getDiskInfo(request,disk_name): partition_list = [] device_name = "" platform_type = platform.platform() if platform_type.startswith("Windows"): partition_info = psutil.disk_partitions() elif platform_type.startswith("Linux"): partition_info = disk_linux_partitions() for items in partition_info: if platform_type.startswith("Windows"): partition_list.append(items.device[:1]) elif platform_type.startswith("Linux"): disk_partition = items.split('/')[-1] partition_list.append(disk_partition) if platform_type.startswith("Windows"): device_name = disk_name + ':\\\\' device_usage = psutil.disk_usage(device_name) disk_used = device_usage.percent disk_free = 100 - device_usage.percent elif platform_type.startswith("Linux"): disk_used = disk_usage_linux(disk_name).split('%')[0] disk_free = 100 - int(disk_used) print 'platform_type',platform_type,partition_info,disk_name disk_name = '\"' + disk_name + '\"' return HttpResponse('{"disk_name":%s,"disk_used": %s,"disk_free":%s}' % (disk_name, disk_used, disk_free))
其中由於psutil的disk_usage()方法不兼容Linux系統,查詢不出對應盤符的使用率,因此我寫了兩個插件linux_disk_partitions.py,linux_disk_usage.py用來在Linux主機上獲取對應盤符的空間佔用信息。
對應代碼linux_disk_usage.py:
#econding=utf-8 #! /usr/bin/env python import os def disk_usage_linux(disk_name): a = os.popen("df -h") for i in a: if disk_name in i.split()[0]: print disk_name,i.split()[4] return i.split()[4] if __name__ == "__main__": disk_usage_linux(disk_name)
linux_disk_partitions.py:
#encoding=utf-8 #!/usr/bin/env python import os def disk_linux_partitions(): partition_list = [] a = os.popen("df -h") for i in a: partition_list.append(i.split()[0]) print partition_list[1:] return partition_list[1:] if __name__ == "__main__": disk_linux_partitions()
最終實現的效果以下圖:
Windows:
Linux:
源碼開放,供各位參考:
https://github.com/jimforit/task_learn