Code: Select all
# usage: tcpProbe <ip> <port> <callback>
#
# Two arguments are appended to the callback command:
# * A status code (1/0 for success/failure)
# * A message (hostname/reason depending on the status code)
proc tcpProbe {ip port callback} {
set sock [socket -async $ip $port]
fileevent $sock writable [list tcpProbed $sock $callback]
set sock
}
proc tcpProbed {sock callback} {
if {[set msg [fconfigure $sock -error]]==""} {
set ok 1
set msg [lindex [fconfigure $sock -peername] 1]
} else {
set ok 0
}
close $sock
eval [lappend callback $ok $msg]
}
# example:
proc showResult {what ok msg} {
if {$ok} {
putlog "connected to $what ($msg)"
} else {
putlog "failed connecting to $what ($msg)"
}
}
tcpProbe 127.0.0.1 1234 [list showResult "the thing"]
Code: Select all
if {[catch {socket $ip $port} re]} {
# connection failed - $re is the reason
} else {
# connected - $re is the socket id
close $re
}
Code: Select all
bind pub - !status sstat:pub
proc sstat:pub {n u h c a} {
global sstat
if {[info exists sstat(pending)]&&$sstat(pending)>0} {
# add channel to target list and wait for pending request
set sstat(target,$c) $n
return
}
if {[info exists sstat(next)]&&$sstat(next)>[clock sec]} {
# use result of previous check
set sstat(target,$c) $n
sstat:msg
} else {
# (re)check servers
set sstat(target,$c) $n
set sstat(pending) 0
# 90 seconds delay before re-checking to prevent flooding
set sstat(next) [expr {[clock sec]+90}]
foreach {name ip port} {
login 192.168.0.2 6900
char 192.168.0.2 6121
map 192.168.0.2 5121
} {
if {[catch {tcpProbe $ip $port [list sstat:sock $name]} err]} {
set sstat(status,$name) "OFFLINE"
putlog "sstat Warning: $err"
} {
incr sstat(pending)
}
}
if {$sstat(pending)==0} {
puthelp "PRIVMSG $c :$n - failed miserably...contact my admin."
}
}
}
proc sstat:sock {name ok x} {
global sstat
if {$ok} {
set sstat(re,$name) "ONLINE"
} {
set sstat(re,$name) "OFFLINE"
}
if {[incr sstat(pending) -1]==0} {
set sstat(msg) "- Server Status \[- Login Server \[ $sstat(re,login) \] - Character Server \[ $sstat(re,char) \] - Map Server \[ $sstat(re,map) \] - Register \[ ajspeedi.ath.cx/ro/login.php \] - Server Name \[ aJRo \] - Service Type ( eAthena ) Domain Host ( aJRo Team ) -"
sstat:msg
}
}
proc sstat:msg {} {
global sstat
foreach name [array names sstat target,*] {
unset sstat($name)
scan $name target,%s chan
puthelp "PRIVMSG $chan :$sstat(msg)"
}
}
# 1st level
# usage: tcpProbe <ip> <port> <callback>
#
# Two arguments are appended to the callback command:
# * A status code (1/0 for success/failure)
# * A message (hostname/reason depending on the status code)
proc tcpProbed {ip port callback} {
set sock [socket -async $ip $port]
fileevent $sock writable [list tcpProbed $sock $callback]
set sock
}
proc tcpProbed {sock callback} {
if {[set msg [fconfigure $sock -error]]==""} {
set ok 1
set msg [lindex [fconfigure $sock -peername] 1]
} else {
set ok 0
}
close $sock
eval [lappend callback $ok $msg]
}
# example:
proc showResult {what ok msg} {
if {$ok} {
putlog "connected to $what ($msg)"
} else {
putlog "failed connecting to $what ($msg)"
}
}
tcpProbed 127.0.0.1 1234 [list showResult "the thing"]
# 2nd level
if {[catch {socket $ip $port} re]} {
# connection failed - $re is the reason
} else {
# connected - $re is the socket id
close $re
}
# 3rd level
bind pub - !status sstat:pub
proc sstat:pub {n u h c a} {
global sstat
if {[info exists sstat(pending)]&&$sstat(pending)>0} {
# add channel to target list and wait for pending request
set sstat(target,$c) $n
return
}
if {[info exists sstat(next)]&&$sstat(next)>[clock sec]} {
# use result of previous check
set sstat(target,$c) $n
sstat:msg
} else {
# (re)check servers
set sstat(target,$c) $n
set sstat(pending) 0
# 90 seconds delay before re-checking to prevent flooding
set sstat(next) [expr {[clock sec]+90}]
foreach {name ip port} {
login 192.168.0.2 6900
char 192.168.0.2 6121
map 192.168.0.2 5121
} {
if {[catch {tcpProbe $ip $port [list sstat:sock $name]} err]} {
set sstat(status,$name) "OFFLINE"
putlog "sstat Warning: $err"
} {
incr sstat(pending)
}
}
if {$sstat(pending)==0} {
puthelp "PRIVMSG $c :$n - failed miserably...contact my admin."
}
}
}
proc sstat:sock {name ok x} {
global sstat
if {$ok} {
set sstat(re,$name) "ONLINE"
} {
set sstat(re,$name) "OFFLINE"
}
if {[incr sstat(pending) -1]==0} {
set sstat(msg) "- Server Status \[- Login Server \[ $sstat(re,login) \] - Character Server \[ $sstat(re,char) \] - Map Server \[ $sstat(re,map) \] - R$
sstat:msg
}
}
proc sstat:msg {} {
global sstat
foreach name [array names sstat target,*] {
unset sstat($name)
scan $name target,%s chan
puthelp "PRIVMSG $chan :$sstat(msg)"
}
}
You didn't just add the two procs...you added my usage example too and the non-blocking example ...AND you somehow got the brilliant idea of renaming one of the procsuser wrote:Add the two probe procs from my first post...
Code: Select all
bind pub - !status sstat:pub
proc sstat:pub {n u h c a} {
global sstat
if {[info exists sstat(pending)]&&$sstat(pending)>0} {
# add channel to target list and wait for pending request
set sstat(target,$c) $n
return
}
if {[info exists sstat(next)]&&$sstat(next)>[clock sec]} {
# use result of previous check
set sstat(target,$c) $n
sstat:msg
} else {
# (re)check servers
set sstat(target,$c) $n
set sstat(pending) 0
# 90 seconds delay before re-checking to prevent flooding
set sstat(next) [expr {[clock sec]+90}]
foreach {name ip port} {
login 192.168.0.2 6900
char 192.168.0.2 6121
map 192.168.0.2 5121
} {
if {[catch {tcpProbe $ip $port [list sstat:sock $name]} err]} {
set sstat(status,$name) "OFFLINE"
putlog "sstat Warning: $err"
} {
incr sstat(pending)
}
}
if {$sstat(pending)==0} {
puthelp "PRIVMSG $c :$n - failed miserably...contact my admin."
}
}
}
proc sstat:sock {name ok x} {
global sstat
if {$ok} {
set sstat(re,$name) "ONLINE"
} {
set sstat(re,$name) "OFFLINE"
}
if {[incr sstat(pending) -1]==0} {
set sstat(msg) "- Server Status \[- Login Server \[ $sstat(re,login) \] - Character Server \[ $sstat(re,char) \] - Map Server \[ $sstat(re,map) \] - Register \[ ajspeedi.ath.cx/ro/login.php \] - Server Name \[ aJRo \] - Service Type ( eAthena ) Domain Host ( aJRo Team ) -"
sstat:msg
}
}
proc sstat:msg {} {
global sstat
foreach name [array names sstat target,*] {
unset sstat($name)
scan $name target,%s chan
puthelp "PRIVMSG $chan :$sstat(msg)"
}
}
# the two procs from my first post:
proc tcpProbe {ip port callback} {
set sock [socket -async $ip $port]
fileevent $sock writable [list tcpProbed $sock $callback]
set sock
}
proc tcpProbed {sock callback} {
if {[set msg [fconfigure $sock -error]]==""} {
set ok 1
set msg [lindex [fconfigure $sock -peername] 1]
} else {
set ok 0
}
close $sock
eval [lappend callback $ok $msg]
}
zfitri wrote:ok thanxs for replying.. sorry i made the mistakes..![]()
now the tcl work.. but the result is not what i want.. i mean.. when the server offline it still reply online... any idea.?
After doing a check, the script will not perform another check for 90 seconds, but instead use the result of the previous check. If you think 90 seconds is too long, change the line below that comment to suit your needs.# 90 seconds delay before re-checking to prevent flooding