Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_flashing.lua
blobc6bd26f5af3df85970b3c563942ea979455cbf74
1 -- Authors: Mario García H. <drosophila@nmental.com>
2 -- License: GPL, version 2
3 -- Last Changed: 2006-12-10
4 --
5 ------------------------------------------------------------------------------------
6 --
7 -- DESCRIPTION:
8 -- [ Multi Purpose Monitor for Ion3]
9 -- It detects if security logs, mboxes, maildirs, some files, etcetera were
10 -- changed. If they were changed in fact, shows a flashing (blinking) alarm with a
11 -- message text specified on settings (or default = !!).
13 -- If you specify your mail inbox it will do a flashing advise of new email.
14 -- If you specify a security log it will reflect your security warnings.
15 --
16 -- You could specify whatever files or directories that do you want to monitor.
17 --> All in Unix* like Oses is a file...
19 -- PLEASE READ THIS:
20 -- * This is another toy for Ion3.
21 -- ** It is not intended to replace or use Gamin, F.A.M. like libs.
22 -- *** This is not a full whistles and sparkles power security monitor like Tripwire.
23 -- **** It is not a good idea to rely on this script to take care of sensible information.
24 -- ***** The level of recursion is only of one level for directories.
25 -- It only provides a very simple way to know if:
27 -- -> Inode number was modified or
28 -- -> Size has changed or
29 -- -> User-group has changed or
30 -- -> Access permisions have changed or
31 -- -> Name changes (including file deletion and moving) then
32 -- ___________________________________________________________
33 -- It flashes a specified message for 'certain specified time'
34 --
35 -- PUPOSE:
36 -- To share a quick method for timers control and blinking patterns on Ion3 statusbars,
37 -- I mean, to fill statusbar(s) with annoying moving things. So don't use it ;]
39 ------- USAGE EXAMPLE : ------------------------------------------------------------
40 --
41 -- If you don't know how to get this working please refer to Ion manual pages,
42 -- Ion home page or some other scripts. Then, write something like this on cfg_statusd.lua :
43 --
44 -- mod_statusbar.create{
45 -- template = "whatever.. %flashing ..whatever", --> Modify this part.
46 -- },
47 --
48 ----> And add somethig like this to:
50 -- mod_statusbar.launch_statusd{
51 --
52 -- flashing = {
53 -- files = {"/mnt/Feed_My_Dog", "~/Mail"} --> The intended purpose files (logs, mail)
54 -- log = ".ion3/flashing.log" --> Some file in your $HOME[...] path.
55 -- NOTE: $HOME is assumed by 'log'.
56 -- Paths not in $HOME are invalid.
57 -- update_interval = 3000, --> Time in milliseconds to update info.
58 -- flash_interval = 300, --> Speed of flashing pattern. (msecs.)
59 -- alarm_message = "!!", --> Flashing Message (defaults are a good bet).
60 -- normal_message = "--", --> Normal status message.
61 -- turn_off = 60, --> This, avoids to show the annoying
62 -- message for ever. The value represents
63 -- }, cycles (10 * flash_interval msecs.)
64 -- }
65 ------------------------------------------------------------------------------------
66 --
67 -- LICENSE: GPL2 Copyright(C)2006 Mario García H.
68 -- (See http://www.gnu.org/licenses/gpl.html to read complete license)
70 -- T.STAMP: Sun Dec 10 02:08:39 2006
72 -- DEPENDS: None at all.
74 -- INSECTS: You are the entomologist. You tell me.
76 -- NOTES ON USAGE:
77 -- - This script creates his own log of activity. You can choose a name and path on settings.
78 -- - If you remove the log, alarms will cease (rm -f *.log). Is not necessary to restart Ion.
79 -- - If you change the settings, the log will be auto-removed and re-written to reflect the changes
80 -- without false alarms.
81 -- - If, for some impossible circumstance, the status of some file or directory is normal again,
82 -- flashing will cease the annoying flashing thing by him self.
83 -- - If alarms are activated on certain time and you exit Ion current session, the
84 -- next session in Ion you will see the annoying thing on your screen... again.
85 -- - If you exit Ion and then you do changes to a file, the next time on Ion the alarms will do blinking.
86 -- - The minimum number for flashing interval (blink) is 300.
87 --
88 -- CONTACT:
89 -- G.H. < drosophila (at) Nmental (dot) com>
91 ------------------------------------------------------------------------------------
93 local defaults = {
94 files = { "~/.ion3" }, --> If you like so much Ion, this is the better default.
95 -- If you change this setting, the log will be auto-updated!
96 log = ".ion3/flashing.log", --> Where do you like to log? Paths not in $HOME [...]
97 -- are invalid. If you change this file, please, remove
98 -- your self the last file used. You are warned.
99 -- NOTE: $HOME is assumed by 'log'.
100 alarm_message = "!!", --> Put here the alarm message.
101 normal_message = "--", --> Put here the normal status message.
103 update_interval = 5*1000, --> Check your files every X milliseconds.
104 flash_interval = 400, --> Blinking interval in milliseconds.
105 turn_off = 500, --> Turn Off the alarm if it annoys you too much time:
106 -- (turn_off*flash_interval) milliseconds.
107 --> If you want permanent alarms: 3*999*999 is OK.
109 local settings = table.join(statusd.get_config("flashing"), defaults)
111 ---SOME INSANE LOCALS :-------------------------------------------------------------
113 local flashing_timer
114 local is_ok = true
115 local flash = false
116 local home = os.getenv("HOME")
117 local message_lenght = string.len(settings.alarm_message)
118 local turn_off = settings.turn_off
119 settings.flash_interval = settings.flash_interval > 299 and settings.flash_interval or 300
121 ---CONFIRM SOME INFO :--------------------------------------------------------------
123 local function confirm_someinfo()
124 local keys = table.concat(settings.files, " * ")
125 local log_file = home.."/"..settings.log
127 local check_status = function()
128 local check = io.popen("ls -liL --color=none "..table.concat(settings.files, " "))
129 local status = check:read("*a"); check:close()
130 return status
133 local new_log = function()
134 local new = io.open(log_file, "w")
135 if not new then
136 return false
137 else
138 new:write(keys.."\n"..check_status())
139 new:close()
140 os.execute("chmod 400 "..log_file)
141 new = io.open(log_file, "r")
142 local log = new:read("*a"); new:close()
143 return log
147 local check_changes = function()
148 local file = io.open(log_file, "r")
149 if not file then
150 return
151 else
152 local changes = file:read("*l"); file:close()
153 return changes ~= keys and
154 os.execute("rm -f "..log_file) or 1
158 if not turn_off then
159 turn_off = settings.turn_off --> Reinitialize turn_off if flash was done
160 is_ok = true
161 os.execute("rm -f "..log_file)
164 check_changes()
166 local file = io.open(log_file, "r")
167 local log = file == nil and new_log() or file:read("*a")
168 is_ok = keys.."\n"..check_status() == log
169 return
173 ---FLASHING PATERNS :---------------------------------------------------------------
175 local function flash_alarm()
176 turn_off = turn_off > 0 and turn_off - 1 or false --> Regresive shut down counter.
177 -- this is an extra ;>
178 local show = function()
179 statusd.inform("flashing", settings.alarm_message)
180 statusd.inform("flashing_hint", "critical")
181 return true
183 local hide = function()
184 statusd.inform("flashing", string.rep(".", message_lenght))
185 return false
187 flash = flash == false and show() or hide() --> Do the real hide and show (flash)
188 return
192 ---FUNCTIONS CALLINGS :-------------------------------------------------------------
194 local function update_timer()
195 confirm_someinfo() --> A function checking something ... and changing between states
196 if is_ok then
197 statusd.inform("flashing", settings.normal_message)
198 statusd.inform("flashing_hint", "normal")
199 flashing_timer:set(settings.update_interval, update_timer) --> Take different update_interval
200 else
201 flashing_timer:set(settings.flash_interval, update_timer) --> Take different update_interval
202 flash_alarm()
203 end
205 ------------------------------------------------------------------------------------
207 flashing_timer = statusd.create_timer()
208 update_timer()