From 49db769b87c79eb14164492a2b2ab7ff245f431c Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 24 Dec 2014 03:48:26 +0800 Subject: [PATCH] update --- main.py | 41 +++++++++++++++++++++++++------------ requirements.txt | 3 ++- templates/monitor_stat.html | 19 +++++++++++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 2ee9707..9ae37d2 100644 --- a/main.py +++ b/main.py @@ -15,10 +15,12 @@ from email.header import Header from email.mime.image import MIMEImage from collections import OrderedDict -from string import Template import psutil import vincent +from jinja2 import Template +from vincent import AxisProperties, PropertySet, ValueRef +import pandas class DB(object): @@ -90,9 +92,15 @@ def __disk_info(): disk_info[dp.mountpoint] = psutil.disk_usage(dp.mountpoint).percent return disk_info - @staticmethod - def __host_info(): - pass + def __host_info(self): + mem = psutil.virtual_memory() + host_info = { + 'host_ip': self.__config['host_ip'], + 'up_time': datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"), + 'cpu_cores': psutil.cpu_count(), + 'mem_total': '{mem_total} MB'.format(mem_total=mem.total / 1024 / 1024) + } + return host_info @staticmethod def __over_threshold(count, value): @@ -160,8 +168,7 @@ def __disk_stat(self): time.sleep(stat_interval) - @staticmethod - def __render_content(template, data): + def __render_content(self, template_name, data): cmd_pattern = 'vega/bin/vg2png {source_file} {target_file}' cpu_stat = data['cpu_stat'] @@ -190,9 +197,11 @@ def __render_content(template, data): print err return False - with open(template) as fh: - template = Template(str(fh.read())) - return template.substitute(cpu_graph_path=cpu_graph_file_name, mem_graph_path=mem_graph_file_name) + host_info = self.__host_info() + disk_info = self.__disk_info() + with open(template_name) as fh: + template = Template(fh.read().decode('utf-8')) + return template.render(host_info=host_info, disk_info=disk_info) def __email_it(self, subject, content, attach_files): msg = MIMEMultipart() @@ -242,25 +251,31 @@ def __email_summary(self): % (table_name, now) for row in self.__db.query(for_select): stat_data[table_name]['used_percent'].append(row[0]) - stat_data[table_name]['created_at'].append(row[1]) + stat_data[table_name]['created_at'].append(datetime.strptime(row[1], '%Y-%m-%d %H:%M:%S')) for_delete = 'DELETE FROM %s WHERE created_at < "%s"' % (table_name, now) self.__db.execute(for_delete) # cpu cpu_stat_data = stat_data['cpu_stat'] - cpu_graph = vincent.Area(cpu_stat_data['used_percent']) + series = pandas.Series(cpu_stat_data['used_percent'], index=cpu_stat_data['created_at']) + cpu_graph = vincent.Area(series) cpu_graph.axis_titles(x=u'Time', y=u'Usage(%)') # cpu_graph.name(u'CPU使用率') + ax = AxisProperties(labels=PropertySet(angle=ValueRef(value=30))) + cpu_graph.axes[0].properties = ax cpu_graph_json = cpu_graph.to_json() # memory mem_stat_data = stat_data['mem_stat'] - mem_graph = vincent.Area(mem_stat_data['used_percent']) + series = pandas.Series(mem_stat_data['used_percent'], index=mem_stat_data['created_at']) + mem_graph = vincent.Area(series) mem_graph.axis_titles(x=u'Time', y=u'Usage(%)') # mem_graph.name(u'内存使用率') + ax = AxisProperties(labels=PropertySet(angle=ValueRef(value=30))) + mem_graph.axes[0].properties = ax mem_graph_json = mem_graph.to_json() - email_content = self.__render_content(template='templates/monitor_stat.html', data={ + email_content = self.__render_content(template_name='templates/monitor_stat.html', data={ 'cpu_stat': {'data': cpu_graph_json, 'target_file_name': 'cpu_graph.png'}, 'mem_stat': {'data': mem_graph_json, 'target_file_name': 'mem_graph.png'} }) diff --git a/requirements.txt b/requirements.txt index 29e5e20..ed28a27 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ psutil vincent -jinja \ No newline at end of file +jinja +pandas \ No newline at end of file diff --git a/templates/monitor_stat.html b/templates/monitor_stat.html index 7863395..fdd903c 100644 --- a/templates/monitor_stat.html +++ b/templates/monitor_stat.html @@ -6,10 +6,29 @@ 服务器监控数据 +
+ 服务器基本信息: + +
+ CPU使用率统计图:
+ 内存使用率统计图:
+
+ 磁盘使用情况: + +
\ No newline at end of file