1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
\section{Writing \command{ion-statusd} monitors}
\label{sec:statusd}
All statusbar meters that do not monitor the internal state of Ion should
go in the separate \command{ion-statusd} program.
Whenever the user requests a meter \codestr{\%foo} or \codestr{\%foo\_bar} to
be inserted in a statusbar, \file{mod\_statusbar} asks \command{ion-statusd}
to load \fnref{statusd_foo.lua} on its search path (same as that for Ion-side
scripts). This script should then supply all meters with the initial part
\codestr{foo}.
To provide this value, the script should simply call \code{statusd.inform}
with the name of the meter and the value as a string.
Additionally the script should provide a 'template' for the meter to
facilitate expected width calculation by \file{mod\_statusbar}, and
may provide a 'hint' for colour-coding the value. The interpretation
of hints depends on the graphical style in use, and currently the
stock styles support the \codestr{normal}, \codestr{important} and
\codestr{critical} hints.
In our example of the 'foo monitor', at script initialisation we might broadcast
the template as follows:
\begin{verbatim}
statusd.inform("foo_template", "000")
\end{verbatim}
To inform \file{mod\_statusbar} of the actual value of the meter and
indicate that the value is critical if above 100, we might write the
following function:
\begin{verbatim}
local function inform_foo(foo)
statusd.inform("foo", tostring(foo))
if foo>100 then
statusd.inform("foo_hint", "critical")
else
statusd.inform("foo_hint", "normal")
end
end
\end{verbatim}
To periodically update the value of the meter, we must use timers.
First we must create one:
\begin{verbatim}
local foo_timer=statusd.create_timer()
\end{verbatim}
Then we write a function to be called whenever the timer expires.
This function must also restart the timer.
\begin{verbatim}
local function update_foo()
local foo= ... measure foo somehow ...
inform_foo(foo)
foo_timer:set(settings.update_interval, update_foo)
end
\end{verbatim}
Finally, at the end of our script we want to do the initial
measurement, and set up timer for further measurements:
\begin{verbatim}
update_foo()
\end{verbatim}
If our scripts supports configurable parameters, the following code
(at the beginning of the script) will allow them to be configured in
\file{cfg\_statusbar.lua} and passed to the status daemon and our script:
\begin{verbatim}
local defaults={
update_interval=10*1000, -- 10 seconds
}
local settings=table.join(statusd.get_config("foo"), defaults)
\end{verbatim}
|