OSDN Git Service

Add message for locale translator
[karesansui/karesansui.git] / karesansui / lib / rrd / memory.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui Core.
5 #
6 # Copyright (C) 2010 HDE, Inc.
7 #
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.
12 #
13
14 import re
15 import datetime
16 import rrdtool
17 import karesansui
18 from karesansui.lib.const import GRAPH_COMMON_PARAM, DEFAULT_LANGS
19 from karesansui.lib.utils import is_readable, generate_phrase
20
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)
24
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"),
29                     )
30
31     for filepath in rrd_filepath:
32         if is_readable(filepath) is False:
33             return ""
34
35     legend_header_label = {"min":_('Min'),
36                            "max":_('Max'),
37                            "ave":_('Ave'),
38                            "last":_('Last'),
39                            }
40
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"))
44         else:
45             legend_header_label[key] = "%s" % (legend_header_label[key].encode("utf-8"))
46
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']
51                                                                                             )
52
53     title = _('Memory')
54     if re.search(u"[^a-zA-Z0-9_\-\. ]", title):
55         title = "%s" % (title.encode("utf-8"))
56     else:
57         title = "<tt>%s</tt>" % (title.encode("utf-8"))
58
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"))
62     else:
63         created_label = "%s" % (created_label.encode("utf-8"))
64
65     created_time = "%s" % (datetime.datetime.today().strftime(DEFAULT_LANGS[lang]['DATE_FORMAT'][1]))
66     created_time = re.sub(r':', '\:', created_time)
67
68     legend_footer = "<tt>%s \: %s</tt>" % (created_label, created_time)
69
70     data = rrdtool.graph(graph_filepath,
71                          GRAPH_COMMON_PARAM,
72                          "--title", title,
73                          # TRANSLATORS:
74                          #  メモリのグラフの縦軸のラベル
75                          #  Byte? Bytes?
76                          "--vertical-label", _('Byte').encode("utf-8"),
77                          "--lower-limit", "0",
78                          "--rigid",
79                          "--start", start,
80                          "--end",  end,
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",
88                          # TRANSLATORS:
89                          #  メモリのグラフの項目名
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),
112                          "COMMENT: \\n",
113                          )
114
115     return graph_filepath