luci-app-statistics: only graph data supported by APC UPS
[project/luci.git] / applications / luci-app-statistics / luasrc / statistics / rrdtool / definitions / apcups.lua
1 -- Copyright 2015 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.statistics.rrdtool.definitions.apcups",package.seeall)
5
6 function rrdargs( graph, plugin, plugin_instance )
7
8         local lu = require("luci.util")
9         local rv = { }
10
11         -- Types and instances supported by APC UPS
12         -- e.g. ups_types -> { 'timeleft', 'charge', 'percent', 'voltage' }
13         -- e.g. ups_inst['voltage'] -> { 'input', 'battery' }
14
15         local ups_types = graph.tree:data_types( plugin, plugin_instance )
16
17         local ups_inst = {}
18         for _, t in ipairs(ups_types) do
19                 ups_inst[t] = graph.tree:data_instances( plugin, plugin_instance, t )
20         end
21
22
23     -- Check if hash table or array is empty or nil-filled
24
25         local function empty( t )
26                 for _, v in pairs(t) do
27                         if type(v) then return false end
28                 end
29                 return true
30         end
31
32
33         -- Append graph definition but only types/instances which are
34         -- supported and available to the plugin and UPS.
35
36         local function add_supported( t, defs )
37                 local def_inst = defs['data']['instances']
38
39                 if type(def_inst) == "table" then
40                         for k, v in pairs( def_inst ) do
41                                 if lu.contains( ups_types, k) then
42                                         for j = #v, 1, -1 do
43                                                 if not lu.contains( ups_inst[k], v[j] ) then
44                                                         table.remove( v, j )
45                                                 end
46                                         end
47                                         if #v == 0 then
48                                                 def_inst[k] = nil  -- can't assign v: immutable
49                                         end
50                                 else
51                                         def_inst[k] = nil  -- can't assign v: immutable
52                                 end
53                         end
54                         if empty(def_inst) then return end
55                 end
56                 table.insert( t, defs )
57         end
58
59
60     -- Graph definitions for APC UPS measurements MUST use only 'instances':
61     -- e.g. instances = { voltage = {  "input", "output" } }
62
63         local voltagesdc = {
64                 title = "%H: Voltages on APC UPS - Battery",
65                 vlabel = "Volts DC",
66                 alt_autoscale = true,
67                 number_format = "%5.1lfV",
68                 data = {
69                         instances = {
70                                 voltage = { "battery" }
71                         },
72                         options = {
73                                 voltage = { title = "Battery voltage", noarea=true }
74                         }
75                 }
76         }
77         add_supported( rv, voltagesdc )
78
79         local voltagesac = {
80                 title = "%H: Voltages on APC UPS - AC",
81                 vlabel = "Volts AC",
82                 alt_autoscale = true,
83                 number_format = "%5.1lfV",
84                 data = {
85                         instances = {
86                                 voltage = {  "input", "output" }
87                         },
88                         options = {
89                                 voltage_output  = { color = "00e000", title = "Output voltage", noarea=true, overlay=true },
90                                 voltage_input   = { color = "ffb000", title = "Input voltage", noarea=true, overlay=true }
91                         }
92                 }
93         }
94         add_supported( rv, voltagesac )
95
96         local percentload = {
97                 title = "%H: Load on APC UPS ",
98                 vlabel = "Percent",
99                 y_min = "0",
100                 y_max = "100",
101                 number_format = "%5.1lf%%",
102                 data = {
103                         instances = {
104                                 percent = { "load" }
105                         },
106                         options = {
107                                 percent_load = { color = "00ff00", title = "Load level"  }
108                         }
109                 }
110         }
111         add_supported( rv, percentload )
112
113         local charge_percent = {
114                 title = "%H: Battery charge on APC UPS ",
115                 vlabel = "Percent",
116                 y_min = "0",
117                 y_max = "100",
118                 number_format = "%5.1lf%%",
119                 data = {
120                         instances = {
121                                 charge = { "" }
122                         },
123                         options = {
124                                 charge = { color = "00ff0b", title = "Charge level"  }
125                         }
126                 }
127         }
128         add_supported( rv, charge_percent )
129
130         local temperature = {
131                 title = "%H: Battery temperature on APC UPS ",
132                 vlabel = "\176C",
133                 number_format = "%5.1lf\176C",
134                 data = {
135                         instances = {
136                                 temperature = { "" }
137                         },
138                         options = {
139                                 temperature = { color = "ffb000", title = "Battery temperature" } }
140                 }
141         }
142         add_supported( rv, temperature )
143
144         local timeleft = {
145                 title = "%H: Time left on APC UPS ",
146                 vlabel = "Minutes",
147                 number_format = "%.1lfm",
148                 data = {
149                         instances = {
150                                 timeleft = { "" }
151                         },
152                         options = {
153                                 timeleft = { color = "0000ff", title = "Time left" }
154                         }
155                 }
156         }
157         add_supported( rv, timeleft )
158
159         local frequency = {
160                 title = "%H: Incoming line frequency on APC UPS ",
161                 vlabel = "Hz",
162                 number_format = "%5.0lfhz",
163                 data = {
164                         instances = {
165                                 frequency = { "input" }
166                         },
167                         options = {
168                                 frequency_input = { color = "000fff", title = "Line frequency" }
169                         }
170                 }
171         }
172         add_supported( rv, frequency )
173
174         return rv
175 end