OSDN Git Service

Add message for locale translator
[karesansui/karesansui.git] / karesansui / lib / rrd / uptime.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 rrdtool
16 import datetime
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_uptime_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/uptime/uptime.rrd" % (rrd_dir),
26                     )
27
28     for filepath in rrd_filepath:
29         if is_readable(filepath) is False:
30             return ""
31
32     legend_header_label = {"last":_('Last'),
33                            }
34
35     for key in legend_header_label.keys():
36         if re.search(u"[^a-zA-Z0-9]", legend_header_label[key]):
37             legend_header_label[key] = "</tt>%s<tt>" % (legend_header_label[key].encode("utf-8"))
38         else:
39             legend_header_label[key] = "   %s" % (legend_header_label[key].encode("utf-8"))
40
41     legend_header = "<tt>                          %s</tt>" % (legend_header_label['last'])
42
43     # TRANSLATORS:
44     #   起動時間のグラフの凡例
45     legend_label = {"uptime":_('uptime'),
46                     "day":_('days'),
47                     "hour":_('hours'),
48                     "minute":_('mins'),
49                     }
50
51     for key in legend_label.keys():
52         if re.search(u"[^a-zA-Z0-9]", legend_label[key]):
53             legend_label[key] = "</tt>%s<tt>" % (legend_label[key].encode("utf-8"))
54         else:
55             legend_label[key] = "%s" % (legend_label[key].encode("utf-8"))
56
57     # TRANSLATORS:
58     #  起動時間のグラフのタイトル
59     title = _('Uptime')
60     if re.search(u"[^a-zA-Z0-9_\-\.]", title):
61         title = "%s" % (title.encode("utf-8"))
62     else:
63         title = "<tt>%s</tt>" % (title.encode("utf-8"))
64
65     created_label = _('Graph created')
66     if re.search(u"[^a-zA-Z0-9 ]", created_label):
67         created_label = "</tt>%s<tt>" % (created_label.encode("utf-8"))
68     else:
69         created_label = "%s" % (created_label.encode("utf-8"))
70
71     created_time = "%s" % (datetime.datetime.today().strftime(DEFAULT_LANGS[lang]['DATE_FORMAT'][1]))
72     created_time = re.sub(r':', '\:', created_time)
73
74     legend_footer = "<tt>%s \: %s</tt>" % (created_label, created_time)
75
76     data = rrdtool.graph(graph_filepath,
77                          GRAPH_COMMON_PARAM,
78                          "--title", title,
79                          # TRANSLATORS:
80                          #  起動時間のグラフの縦軸のラベル
81                          "--vertical-label", _('Days').encode("utf-8"),
82                          "--lower-limit", "0",
83                          "--rigid",
84                          "--start", start,
85                          "--end",  end,
86                          "--alt-autoscale",
87                          "--legend-direction", "bottomup",
88                          "DEF:uptime=%s:value:AVERAGE" % (rrd_filepath[0]),
89                          "CDEF:day=uptime,86400,/",
90                          "CDEF:hour=uptime,86400,%,3600,/",
91                          "CDEF:minute=uptime,3600,%,60,/",
92                          "COMMENT:%s\\r" % legend_footer,
93                          "COMMENT:<tt>---------------------------------------------------------------------------</tt>\\n",
94                          "AREA:day#80AA00:<tt>%s    </tt>" % (legend_label["uptime"]),
95                          "GPRINT:day:LAST:<tt>%%6.0lf %s</tt>" % (legend_label["day"]),
96                          "GPRINT:hour:LAST:<tt>%%2.0lf %s</tt>" % (legend_label["hour"]),
97                          "GPRINT:minute:LAST:<tt>%%2.0lf %s</tt>\\n" % (legend_label["minute"]),
98                          "COMMENT:%s\\n" % (legend_header),
99                          "COMMENT: \\n",
100                          )
101
102     return graph_filepath