2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui Core.
6 # Copyright (C) 2010 HDE, Inc.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
18 from karesansui.lib.const import GRAPH_COMMON_PARAM, DEFAULT_LANGS
19 from karesansui.lib.utils import is_readable, generate_phrase
21 def create_memory_graph(_, lang, graph_dir, rrd_dir, start, end, dev=None, type=None):
22 graph_filename = "%s.png" % (generate_phrase(12,'abcdefghijklmnopqrstuvwxyz'))
23 graph_filepath = '%s/%s' % (graph_dir, graph_filename)
25 rrd_filepath = ("%s/memory/memory-%s.rrd" % (rrd_dir, "free"),
26 "%s/memory/memory-%s.rrd" % (rrd_dir, "cached"),
27 "%s/memory/memory-%s.rrd" % (rrd_dir, "buffered"),
28 "%s/memory/memory-%s.rrd" % (rrd_dir, "used"),
31 for filepath in rrd_filepath:
32 if is_readable(filepath) is False:
35 legend_header_label = {"min":_('Min'),
41 for key in legend_header_label.keys():
42 if re.search(u"[^a-zA-Z0-9]", legend_header_label[key]):
43 legend_header_label[key] = "</tt>%s<tt>" % (legend_header_label[key].encode("utf-8"))
45 legend_header_label[key] = "%s" % (legend_header_label[key].encode("utf-8"))
47 legend_header = "<tt> %s %s %s %s</tt>" % (legend_header_label['min'],
48 legend_header_label['max'],
49 legend_header_label['ave'],
50 legend_header_label['last']
54 if re.search(u"[^a-zA-Z0-9_\-\. ]", title):
55 title = "%s" % (title.encode("utf-8"))
57 title = "<tt>%s</tt>" % (title.encode("utf-8"))
59 created_label = _('Graph created')
60 if re.search(u"[^a-zA-Z0-9 ]", created_label):
61 created_label = "</tt>%s<tt>" % (created_label.encode("utf-8"))
63 created_label = "%s" % (created_label.encode("utf-8"))
65 created_time = "%s" % (datetime.datetime.today().strftime(DEFAULT_LANGS[lang]['DATE_FORMAT'][1]))
66 created_time = re.sub(r':', '\:', created_time)
68 legend_footer = "<tt>%s \: %s</tt>" % (created_label, created_time)
70 data = rrdtool.graph(graph_filepath,
76 "--vertical-label", _('Byte').encode("utf-8"),
81 "--legend-direction", "bottomup",
82 "DEF:free=%s:value:AVERAGE" % (rrd_filepath[0]),
83 "DEF:cached=%s:value:AVERAGE" % (rrd_filepath[1]),
84 "DEF:buffered=%s:value:AVERAGE" % (rrd_filepath[2]),
85 "DEF:used=%s:value:AVERAGE" % (rrd_filepath[3]),
86 "COMMENT:%s\\r" % legend_footer,
87 "COMMENT:<tt>---------------------------------------------------------------------------</tt>\\n",
90 # 日本語にした場合は表示が崩れますが、後で直すのでそのままで大丈夫です
91 "AREA:used#80AA00:<tt>%s </tt>" % (_('Used').encode("utf-8")),
92 "GPRINT:used:MIN:<tt>%8.1lf %s</tt>",
93 "GPRINT:used:MAX:<tt>%8.1lf %s</tt>",
94 "GPRINT:used:AVERAGE:<tt>%8.1lf %s</tt>",
95 "GPRINT:used:LAST:<tt>%8.1lf %s</tt>\\n",
96 "STACK:buffered#E7EF00:<tt>%s </tt>" % (_('Buffered').encode("utf-8")),
97 "GPRINT:buffered:MIN:<tt>%8.1lf %s</tt>",
98 "GPRINT:buffered:MAX:<tt>%8.1lf %s</tt>",
99 "GPRINT:buffered:AVERAGE:<tt>%8.1lf %s</tt>",
100 "GPRINT:buffered:LAST:<tt>%8.1lf %s</tt>\\n",
101 "STACK:cached#B3EF00:<tt>%s </tt>" % (_('Cached').encode("utf-8")),
102 "GPRINT:cached:MIN:<tt>%8.1lf %s</tt>",
103 "GPRINT:cached:MAX:<tt>%8.1lf %s</tt>",
104 "GPRINT:cached:AVERAGE:<tt>%8.1lf %s</tt>",
105 "GPRINT:cached:LAST:<tt>%8.1lf %s</tt>\\n",
106 "STACK:free#FFFFFF:<tt>%s </tt>" % (_('Free').encode("utf-8")),
107 "GPRINT:free:MIN:<tt>%8.1lf %s</tt>",
108 "GPRINT:free:MAX:<tt>%8.1lf %s</tt>",
109 "GPRINT:free:AVERAGE:<tt>%8.1lf %s</tt>",
110 "GPRINT:free:LAST:<tt>%8.1lf %s</tt>\\n",
111 "COMMENT:%s\\n" % (legend_header),
115 return graph_filepath