-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcl-monitord
271 lines (225 loc) · 7.28 KB
/
tcl-monitord
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#! /bin/echo The following script is an Eggdrop script:
package require json
source jimson.tcl
proc putwarning {script message {letter W}} {
putlog [format {(%s) %s: %s} $script $letter $message]
}
namespace eval ::perl {
if {[catch {package require lexec} err]} {
putwarning tcl-monitord "load lexec failed. Perl support risky."
proc ::perl::exec {args} {::exec {*}$args}
} {
package require lexec
proc ::perl::exec {args} {::lexec::exec | $args}
}
::proc apply {script args} {
exec perl -e $script {*}$args
}
::proc alias {name script} {
::proc $name {args} [list ::perl::exec perl -e $script {*}$args]
}
#Want to split this off some day.
}
#perl -MJSON -MProc::ProcessTable -e 'print to_json ( [ map {{ %$_ }} @{ Proc::ProcessTable->new->table } ], { pretty => 1 } );' | less
proc istrue {v} {
expr {[string tolower $v] == "true" || $v == "1"}
}
proc tclmon-module {name script} {
namespace eval ::tclmon::modules::$name [list set modname $name]
namespace eval ::tclmon::modules_pv::$name [list set modname $name]
namespace eval ::tclmon::modules::$name {
proc m {a b} {
variable modname
proc $a {n uh h c t} $b
# /!\ Veritably an interesting hack to attempt to parley a public
# proc into a private proc. XXX /!\
namespace eval ::tclmon::modules_pv::$modname {}
proc ::tclmon::modules_pv::${modname}::$a {n uh h t} [format "::tclmon::modules::%s::%s %s\n" $modname $a {$n $uh $h $n $t}]
}
proc func {name script} {
m $name $script
}
# /!\ XXX /!\ hack alert
proc mgetconf {args} [format {::tclmon::gmconf %s %s} $modname {{*}$args}]
foreach i $::tclmon::commands {
namespace import [format {::tclmon::%s} $i]
}
}
namespace eval ::tclmon::modules::$name $script
}
namespace eval ::tclmon {
# An eggdrop script to allow easier monitoring of the system.
foreach {v} {config unixtype reporthost help prefixes infixes commands} {variable $v}
variable commands [list deformat putprivmsg gmconf getconf pub2msg func m json2var]
namespace export {*}$commands
namespace ensemble create
proc ::tclmon::istrue {v} {
expr {[string tolower $v] == "true" || $v == "1"}
}
proc ::tclmon::ifistrue {v args} {
if {[istrue $v]} {{*}$args}
}
proc ::tclmon::processcommands {} {
foreach {v} {config unixtype reporthost help prefixes nohostprefix infixes} {variable $v}
set commands [getconf commands]
foreach {k v} $commands {
foreach {perms public procedure} $v {break}
foreach prefix $nohostprefix {
uplevel 1 source [format {monitord/%s.tcl} [lindex [set proce [split $procedure "/"]] 0]]
set com [format {%s%s} $prefix $k]
set ecorp [lreverse $proce]
set vpecorp $ecorp
lappend ecorp modules
lappend ecorp tclmon
lappend vpecorp modules_pv
lappend vpecorp tclmon
set proce [lreverse $ecorp]
set procepv [lreverse $vpecorp]
unset ecorp
unset vpecorp
catch {unbind msg $perms $com [join $procepv "::"]}
catch {unbind pub $perms $com [join $proce "::"]}
bind msg $perms $com [join $procepv "::"]
ifistrue $public bind pub $perms $com [join $proce "::"]
}
foreach prefix $prefixes {
foreach infix $infixes {
foreach host [aliashosts] {
uplevel 1 source [format {monitord/%s.tcl} [lindex [set proce [split $procedure "/"]] 0]]
set com [format {%s%s%s%s} $prefix $host $infix $k]
set ecorp [lreverse $proce]
set vpecorp $ecorp
lappend ecorp modules
lappend ecorp tclmon
lappend vpecorp modules_pv
lappend vpecorp tclmon
set proce [lreverse $ecorp]
set procepv [lreverse $vpecorp]
unset ecorp
unset vpecorp
catch {unbind msg $perms $com [join $procepv "::"]}
catch {unbind pub $perms $com [join $proce "::"]}
bind msg $perms $com [join $procepv "::"]
ifistrue $public bind pub $perms $com [join $proce "::"]
}
}
}
}
}
proc ::tclmon::unbindcommands {} {
foreach {v} {config unixtype reporthost nohostprefix help prefixes infixes} {variable $v}
set commands [getconf commands]
foreach {k v} $commands {
foreach {perms public procedure} $v {break}
foreach prefix $nohostprefix {
uplevel 1 source [format {monitord/%s.tcl} [lindex [set proce [split $procedure "/"]] 0]]
set com [format {%s%s} $prefix $k]
set ecorp [lreverse $proce]
set vpecorp $ecorp
lappend ecorp modules
lappend ecorp tclmon
lappend vpecorp modules_pv
lappend vpecorp tclmon
set proce [lreverse $ecorp]
set procepv [lreverse $vpecorp]
unset ecorp
unset vpecorp
catch {unbind msg $perms $com [join $procepv "::"]}
catch {unbind pub $perms $com [join $proce "::"]}
}
foreach prefix $prefixes {
foreach infix $infixes {
foreach host [aliashosts] {
set proce [split $procedure "/"]
set com [format {%s%s%s%s} $prefix $host $infix $k]
set ecorp [lreverse $proce]
set vpecorp $ecorp
lappend ecorp modules
lappend ecorp tclmon
lappend vpecorp modules_pv
lappend vpecorp tclmon
set proce [lreverse $ecorp]
set procepv [lreverse $vpecorp]
unset ecorp
unset vpecorp
catch {unbind msg $perms $com [join $procepv "::"]}
catch {unbind pub $perms $com [join $proce "::"]}
}
}
}
}
}
proc ::tclmon::deformat {str} {
variable reporthost
string map [list %c "\x03" %b "\x02" %o "\x0f" %h $reporthost] $str
}
proc ::tclmon::putprivmsg {targ mod msg {tn {}}} {
variable reporthost
if {$tn == {}} {set targnick ""} {set targnick [format {%s%s%s } [getconf precomma] $tn [getconf comma]]}
set line [string map [list %c "\x03" %b "\x02" %o "\x0f" %h $reporthost %u $mod %m $msg %n $targnick] [getconf msgfmt]]
if {![validchan $targ] && [getconf notice_pv] == 1} {set comd NOTICE} {set comd PRIVMSG}
putquick [format "%s %s :%s" $comd $targ $line]
}
proc ::tclmon::pub2msg {procedure args} {
foreach {n uh h t} $args {break}
set c $n
$procedure $n $uh $h $c $t
}
proc ::tclmon::gmconf {module args} {
getconf modules $module {*}$args
}
proc ::tclmon::prerehsh {{ign {}}} {
unbindcommands
}
proc ::tclmon::rehsh {{ign {}}} {
foreach {k v} {
comma :
precomma ""
msgfmt "\[%c13%h->%c06%u%c\] %n%m"
prefixchar @!
infix {: ->}
notice_pv 1
} {
dict set ::tclmon::config $k $v
}
foreach {k v} [dejsonate [format {./%s.monitord.conf} ${::database-basename}]] {
dict set ::tclmon::config $k $v
}
set ::tclmon::reporthost [dict get $::tclmon::config hostname]
set ::tclmon::prefixes [split [getconf prefixchar] {}]
set ::tclmon::nohostprefix [split [getconf nohostprefix] {}]
set ::tclmon::infixes [getconf infix]
processcommands
}
proc ::tclmon::dejsonate {path} {
set fd [open $path r]
set data [read $fd]
close $fd
::json::json2dict $data
}
proc ::tclmon::json2var {path {varia ""}} {
if {$varia == ""} {return [list]}
set fd [open $path r]
set data [read $fd]
close $fd
upvar 1 $varia v
set v [::json::json2dict $data]
}
foreach opt {o m r i n} {
dict set unixtype $opt [::perl::exec uname -$opt]
}
proc ::tclmon::getconf {args} {
variable config
dict get $::tclmon::config {*}$args
}
proc ::tclmon::aliashosts {} {
variable config
set longform [dict get $config hostname]
set shorts [dict get $config shorthost]
lappend shorts $longform
return $shorts
}
#rehsh
bind evnt - prerehash [namespace current]::prerehsh
bind evnt - rehash [namespace current]::rehsh
};# namespace eval ::tclmon