I was able to find the watchdog-enabled botchecker..
The watchdog.tcl script should be loaded in your bot, and will simply update the atime timestamp of the pidfile once every minute.
The botchk.tcl script should be invoked from crontab or such. It will do a series of tests to determine whether your eggdrop is still running, or if it's frozen, and tries to take appropriate measures.
Unfortunately, due to the way filesystem operation works, this script will also alter the atime timestamp whenever accessing the pidfile, and thus must not be called more often than the hung/gone/dead-time. Recommended is 6+ minutes, I use 10 myself. You might get away with 5 minutes.
There is a tweak in the end of the script that tries to restore the timestamp, however it depends on an external application (touch), and is thus commented out by default. Should touch be available, you'll be able to run the script more often.
watchdog.tcl
Code: Select all
#Watchdog part of botchk.tcl
#Simply makes eggdrop update the accesstime for it's pidfile
#on a regular basis... (kinda like "touch")
#Settings:
# botpid [path/]pid.bot
# Tells the script which file that is the pidfile...
# This should be autodetected by the script at startup,
# but under certain circumstances, it might fail..
# IF it does fail, just set this var manually, and it'll
# work just fine :)
# PS. The name of the pidfile is pid.$botnet-nick, or; if
# botnet-nick isn't set; pid.$nick DS
#set botpid pid.lamestbot
if {![info exists botpid]} {
if {[info exists botnet-nick]} {
set botpid "pid.${botnet-nick}"
putlog "Setting botpid to $botpid using \$botnet-nick"
} elseif {[info exists nick] && $nick != ""} {
set botpid "pid.$nick"
putlog "Setting botpid to $botpid using \$nick"
} else {
error "Unable to determine the name of the pid-file!\nPlease check your config-file or watchdog.tcl script..."
}
}
proc touch {file} {
set fileID [open $file "RDONLY CREAT"]
catch {gets $fileID}
close $fileID
}
proc watchdog_touch {min hour day month year} {
global botpid
touch $botpid
# putlog "Touching $botpid"
}
bind time - "* * * * *" watchdog_touch
botchk.tcl
Code: Select all
#!/usr/bin/tclsh
### settings ###
#pidfile "pidfile of bot"
#set pidfile pid.botname
#userfile "userfile of bot"
#set userfile botname.user
#configfile "configfile of bot"
#set configfile botconfig
#botdir "home of your eggdrop"
#set botdir /home/somewhere
#silent 0/1
# Should we write eggdrop's output from it's startup to stdout?
set silent 0
#lockfile "file to prevent start of your eggdrop"
# Use this whenever you want to stop your bot for a longer while...
set lockfile /home/somewhere/some.lock
### Code ###
#Lets just check that our friendly user supplied all required settings :)
foreach {var what} [list pidfile "Name of pidfile" userfile "Name of userfile" configfile "Name of configfile" botdir "home of your eggdrop" silent "Whether or not to write any output from eggdrop to stdout during startup of bot" lockfile "file to prevent start of your eggdrop"] {
if {![info exists $var]} {
puts stdout "Hey buddy!\nYou messed up while configurating the botchk.tcl script!"
puts stdout "Variable not set: $var - Explanation: $what"
exit 1
}
}
#Lets check whether we should check the bot at all...
if {[file exists $lockfile]} {
exit 0
}
#proc: start_bot
#args: none
#desc: Checks whether the userfile exists
# (if not, it will try to restore it from
# backups, etc...) and then start up
# the bot again.
# Writes the output from eggdrop to stdout...
# (if selected)
proc start_bot {} {
global pidfile userfile configfile silent
if {![file exists $userfile]} {
if {[file exists "${userfile}~new"]} {
file copy "${userfile}~new" $userfile
} elseif {[file exists "${userfile}~bak"]} {
file copy "${userfile}~bak" $userfile
} else {
puts stdout "Error: Can't find any userfile or backupfile!"
exit 1
}
}
if {[file executable $configfile]} {
catch {exec ./${configfile}} temp
} {
catch {exec ./eggdrop $configfile} temp
}
if {$silent == 0} {
puts stdout $temp
}
}
#proc: restart_bot
#args: none
#desc: Checks if there's a bot running (suspected zombie),
# if so it kills it, then calls start_bot
proc restart_bot {} {
global pidfile userfile configfile
set fileID [open $pidfile "RDONLY"]
set temp [gets $fileID botpid]
close $fileID
file delete $pidfile
if {[file exists ${pidfile}~]} {
file delete ${pidfile}~
}
if {$temp > 0} {
puts stdout "Read pid: $botpid Status: " nonewline
if {[file exists "/proc/$botpid"] && [file owned "/proc/$botpid"]} {
puts stdout "Exists!\nChecking if it matches our eggdrop: "
set fileID [open "/proc/${botpid}/cmdline" "RDONLY"]
gets $fileID temp
close $fileID
if {[string compare "$temp" "eggdrop\000./${configfile}"] == 0} {
puts stdout "Matched! - killing..."
puts stdout [exec kill -9 $botpid]
}
} {
puts stdout "no such pid!"
}
}
start_bot
}
#Lets go to bot's home...
cd $botdir
#Check if the pidfile exists, if not, call start_bot
if {![file exists $pidfile]} {
puts stdout "Pidfile $pidfile does not exists!\nGuess bot is not running... Better start her up..."
start_bot
} {
#Good, the pidfile is there... Lets check how old it is..
#(4 minutes and 50 seconds should be enough...)
if {[set time [expr [clock seconds] - [set atime [file atime $pidfile]]]] > 290} {
#Too old for comfort... Lets see if this is the second time in a row that file is too old...
#(to prevent any problems caused by ex. change of the system clock...)
#(if pidfile~ exists, it's the second time in a row that the pid-file is too old...)
puts stdout "Pidfile $pidfile is old... (Havn't been touched in [expr $time/60] minutes)\nChecking for ${pidfile}~... " nonewline
if {[file exists "${pidfile}~"]} {
#It's there... Lets see if it's "real" (should contain the same pid as the real pidfile)
#Danger: reading the pidfile will change it's time, so for this script to work, the "age" check
# must be less than the interval botchk.tcl is called (currently 10 secs lower than 5 minutes,
# change "> 290" some lines above to something lower if you get problems)
# (To put it in other words, don't call botchk.tcl more often than 5 minutes (unless you decrease
# the "> 290"...)
puts stdout "Found!\nValidating ${pidfile}~... " nonewline
set fileID [open "${pidfile}~" "RDONLY"]
gets $fileID pid1
close $fileID
set fileID [open $pidfile "RDONLY"]
gets $fileID pid2
close $fileID
#It's valid... lets restart bot...
if {$pid1 == $pid2} {
puts stdout "Valid!\nRestarting..."
restart_bot
} {
#It's not valid... better remove it...
puts stdout "Not valid - removing... " nonewline
file delete -- "${pidfile}~"
}
} {
#It doesn't exist... lets create it so that we know we've already had this
#problem the next time we check...
puts stdout "Not found!\nCreating new ${pidfile}~... " nonewline
file copy -- $pidfile "${pidfile}~"
}
} {
#pidfile is current, lets remove any stray pidfile~...
if {[file exists ${pidfile}~]} {
file delete -- "${pidfile}~"
}
}
}
#tweak to restore the timestamp after reading...
#depends on touch (havn't found anything in tcl that'll do the trick :/ )
#catch {exec touch -t [clock format $atime -format "%Y%m%d%H%M.%S"] $pidfile} msg