Advertisement
tuvok81

egg überwachung

Jan 19th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.86 KB | None | 0 0
  1. #! /bin/sh
  2. #
  3. # Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
  4. # Copyright (C) 2004-2024 Eggheads Development Team
  5. #
  6. # systemd formatting contributed by PeGaSuS
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. # This trick is borrowed from Tothwolf's Wolfpack \
  23. # Check for working 'grep -E' before using 'egrep' \
  24. if echo a | (grep -E '(a|b)') >/dev/null 2>&1; \
  25. then \
  26.   egrep="grep -E"; \
  27. else \
  28.   egrep=egrep; \
  29. fi; \
  30. # Search for tclsh[0-9].[0-9] in each valid dir in PATH \
  31. for dir in $(echo $PATH | sed 's/:/ /g'); \
  32. do \
  33.   if test -d $dir; \
  34.   then \
  35.     files=$(/bin/ls $dir | $egrep '^tclsh[0-9]\.[0-9]$'); \
  36.     if test "$files" != ""; \
  37.     then \
  38.       versions="${versions:+$versions }$(echo $files | sed 's/tclsh//g')"; \
  39.     fi; \
  40.   fi; \
  41. done; \
  42. for ver in $versions; \
  43. do \
  44.   tmpver=$(echo $ver | sed 's/\.//g'); \
  45.   if test "$lasttmpver" != ""; \
  46.   then \
  47.     if test "$tmpver" -gt "$lasttmpver"; \
  48.     then \
  49.       lastver=$ver; \
  50.       lasttmpver=$tmpver; \
  51.     fi; \
  52.   else \
  53.     lastver=$ver; \
  54.     lasttmpver=$tmpver; \
  55.   fi; \
  56. done; \
  57. exec tclsh$lastver "$0" ${1+"$@"}
  58. #
  59. # AutoBotchk - An eggdrop utility to autogenerate botchk/crontab entries
  60. #
  61. # Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
  62. # Copyright (C) 2004-2024 Eggheads Development Team
  63. #
  64. # How to use
  65. # ----------
  66. #
  67. #   Most people begin to use AutoBotchk by moving it from the script
  68. # directory to their Eggdrop directory -- this will save you from having to
  69. # use the -dir option.
  70. #
  71. #   If you run AutoBotchk without any arguments, it will present you with
  72. # a list of valid ones. Most people run AutoBotchk by doing:
  73. #
  74. #     ./autobotchk <config file>
  75. #
  76. #   This will setup crontab to check every 10 minutes to see whether or not
  77. # your bot needs to be restarted and it will e-mail if a restart was
  78. # performed. A lot of people turn off crontab e-mail support; however, I do
  79. # not recommend this since you will be unable to see any errors that might
  80. # happen.
  81. #
  82. # Updates
  83. # -------
  84. #   27Sep2001: added new pidfile setting
  85. #   14Nov2001: removed old autobotchk update entries and updated the help
  86. #              section a little bit. also made autobotchk move down one
  87. #              directory if being run from the scripts directory.
  88. #   15Apr2003: cleaned up a few things, fixed a few bugs, and made a little
  89. #              love! j/k
  90. #   13Dec2016: add possibility to use multiple confs; fixes for (botnet-)nick
  91. #              with len > handlen, for (botnet-)nicks with \[ \] and for
  92. #              $(botnet-)nick used in pidfile, userfile or (botnet-)nick
  93. #   19Apr2017: Fix running this from a non-eggdrop dir.
  94. #   28Nov2022: Added -systemd option, to (duh) create a systemd job
  95.  
  96. if {$argc == 0} {
  97.   puts "\nusage: $argv0 <eggdrop mainconfig> \[options\] \[additional configs\]"
  98.   puts "Options:"
  99.   puts "--------"
  100.   puts " systemd options:"
  101.   puts "  -systemd (install systemd job instead of cron)"
  102.   puts ""
  103.   puts " crontab options:"
  104.   puts "  -dir     (directory to run autobotchk in)"
  105.   puts "  -noemail (discard crontab e-mails)"
  106.   puts "  -5       (5 minute checks)"
  107.   puts "  -10      (10 minute checks)"
  108.   puts "  -15      (15 minute checks)"
  109.   puts "  -30      (30 minute checks)"
  110.   puts ""
  111.   puts "Additional Configs:"
  112.   puts "-------------------"
  113.   puts "  If you source additional configs from your main config and they"
  114.   puts "  contain either nick, botnet-nick, userfile or pidfile settings,"
  115.   puts "  list them here so that autobotchk can check in them as well."
  116.   puts ""
  117.   exit
  118. }
  119.  
  120. fconfigure stdout -buffering none
  121.  
  122. proc newsplit {text {split " "}} {
  123.   upvar $text ours
  124.   append ours $split
  125.   set index [string first $split $ours]
  126.   if {$index == -1} {
  127.     set ours ""
  128.     return ""
  129.   }
  130.   set tmp [string trim [string range $ours 0 $index]]
  131.   set ours [string trim [string range $ours [expr $index + [string length $split]] end]]
  132.   return $tmp
  133. }
  134.  
  135. puts "\nautobotchk 1.11, (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)"
  136. puts "                   (C) 2004-2022 Eggheads Development Team"
  137. puts "------------------------------------------------------------\n"
  138.  
  139. set mainconf [newsplit argv]
  140. set confs [list $mainconf]
  141. set dir [pwd]
  142. set delay 10
  143. set email 1
  144. set systemd 0
  145. catch {exec which kill} kill
  146.  
  147. # If you renamed your eggdrop binary, you should change this variable
  148. set binary "eggdrop"
  149.  
  150. while {[set opt [newsplit argv]] != ""} {
  151.   switch -- $opt {
  152.     "-systemd" { set systemd 1 }
  153.     "-time" -
  154.     "-1" { set delay 1 }
  155.     "-5" { set delay 5 }
  156.     "-10" { set delay 10 }
  157.     "-15" { set delay 15 }
  158.     "-20" { set delay 20 }
  159.     "-30" { set delay 30 }
  160.     "-nomail" -
  161.     "-noemail" {set email 0}
  162.     "-dir" {
  163.       set dir [newsplit argv]
  164.       if {[string match -* $dir]} {
  165.         puts "*** ERROR: you did not supply a directory name"
  166.         puts ""
  167.         exit
  168.       }
  169.       if {![file isdirectory $dir]} {
  170.         puts "*** ERROR: the directory you supplied is not a directory"
  171.         puts ""
  172.         exit
  173.       }
  174.     }
  175.     default {
  176.       # Treat as extra config file
  177.       if {[file isfile $opt] && [file readable $opt]} { lappend confs $opt }
  178.     }
  179.   }
  180. }
  181.  
  182. switch -- $delay {
  183.   "30" { set minutes "0,30" }
  184.   "20" { set minutes "0,20,40" }
  185.   "15" { set minutes "0,15,30,45" }
  186.   "5" { set minutes "0,5,10,15,20,25,30,35,40,45,50,55" }
  187.   "1" { set minutes "*" }
  188.   default { set minutes "0,10,20,30,40,50" }
  189. }
  190.  
  191. if {[string match "*/scripts" $dir]} {
  192.   set dir [string range $dir 0 [expr [string length $dir] - 8]]
  193. }
  194.  
  195. set dir [string trimright $dir /]
  196.  
  197. if {![file exists $dir/help] || ![file isdirectory $dir/help]} {
  198.   puts "*** ERROR: are you sure you are running from a bot directory?"
  199.   puts ""
  200.   exit
  201. } elseif {![file exists $dir/$binary]} {
  202.   puts "*** ERROR: are you sure you are running from a bot directory?"
  203.   puts ""
  204.   exit
  205. }
  206.  
  207. foreach config $confs {
  208.   puts -nonewline "Opening '$config' for processing ... "
  209.  
  210.   if {[catch {open $dir/$config r} fd]} {
  211.     puts "error:"
  212.     puts "  $fd\n"
  213.     exit
  214.   } else {
  215.     puts "done"
  216.   }
  217.  
  218.   set count 0
  219.   puts -nonewline "Scanning the config file "
  220.  
  221.   while {![eof $fd]} {
  222.     incr count
  223.     if {$count == 100} {
  224.       puts -nonewline "."
  225.       set count 0
  226.     }
  227.     set line [gets $fd]
  228.     if {[set blarg [newsplit line]] != "set"} {
  229.       continue
  230.     }
  231.     switch -- [set opt [newsplit line]] {
  232.       "pidfile" -
  233.       "nick" -
  234.       "userfile" -
  235.       "botnet-nick" {
  236.         set $opt [string trim [newsplit line] " \"{}"]
  237.       }
  238.     }
  239.   }
  240.   close $fd
  241.  
  242.   if {$count != 0} {
  243.     puts -nonewline "."
  244.   }
  245.  
  246.   puts " done"
  247. }
  248.  
  249.  if {![info exists {botnet-nick}] && [info exists nick]} {
  250.   puts "  Defaulting \$botnet-nick to \"$nick\""
  251.   set botnet-nick $nick
  252.  }
  253.  if {![info exists {botnet-nick}] || ![info exists userfile]} {
  254.   puts "  *** ERROR: could not find either \$userfile or \$botnet-nick"
  255.   puts ""
  256.   puts "  Are you sure this is a valid eggdrop config file?"
  257.   puts ""
  258.   exit
  259.  }
  260.  catch {exec "$dir/$binary" -v} execres
  261.  if {![regexp {handlen=([0-9]+)} "$execres" -> handlen]} {
  262.   puts "  Could not find handlen used, defaulting to 32."
  263.   # len 32 means up to index 31
  264.   set handlen 31
  265.  } else {
  266.   set handlen [expr $handlen - 1]
  267.  }
  268.  set nick [string range [subst -nocommands $nick] 0 $handlen]
  269.  set botnet-nick [string range [subst -nocommands ${botnet-nick}] 0 $handlen]
  270.  
  271. ### systemd stuff
  272. if {$systemd} {
  273.  puts "Enabling user lingering..."
  274.  if {[catch {exec loginctl enable-linger} res]} {
  275.   puts "Oops, something went wrong. Is systemd running on this system?"
  276.   exit
  277.  }
  278.  puts "Creating systemd directory..."
  279.  catch {file mkdir "$::env(HOME)/.config/systemd/user" } res
  280.  if {[catch {open "$::env(HOME)/.config/systemd/user/${botnet-nick}.service" w} fd]} {
  281.   puts "  *** ERROR: unable to open '${botnet-nick}.service' for writing"
  282.   puts ""
  283.   exit
  284.  }
  285.  puts $fd "### Eggdrop systemd unit, generated by autosystemd"
  286.  puts $fd ""
  287.  puts $fd "\[Unit\]"
  288.  puts $fd "Description=${botnet-nick} (Eggdrop)"
  289.  puts $fd "After=default.target"
  290.  puts $fd ""
  291.  puts $fd "\[Service\]"
  292.  puts $fd "WorkingDirectory=${dir}"
  293.  puts $fd "ExecStart=${dir}/eggdrop ${config}"
  294.  puts $fd "ExecReload=${kill} -s HUP \$MAINPID"
  295.  puts $fd "Type=forking"
  296.  puts $fd "Restart=on-abnormal"
  297.  puts $fd ""
  298.  puts $fd "\[Install\]"
  299.  puts $fd "WantedBy=default.target"
  300.  close $fd
  301.  
  302.  catch {exec systemctl --user enable ${botnet-nick}.service} res
  303.  puts $res
  304.  puts "systemd job successfully installed as '${botnet-nick}.service'."
  305.  puts "* Use 'systemctl --user <start|stop|restart|reload|enable|disable> ${botnet-nick}.service' to control your Eggdrop"
  306.  puts "Starting Eggdrop..."
  307.  if {[catch {exec systemctl --user start ${botnet-nick}.service} res]} {
  308.   puts "ERROR: Eggdrop did not start."
  309.   puts $res
  310.  } else {
  311.   puts "Success."
  312.  }
  313.  exit
  314. }
  315.  
  316.  
  317. if {![info exists pidfile]} {
  318.  puts "  Defaulting \$pidfile to \"pid.${botnet-nick}\""
  319.  set pidfile "pid.${botnet-nick}"
  320. }
  321. set pidfile [subst -nocommands $pidfile]
  322. if {[catch {open $dir/${botnet-nick}.botchk w} fd]} {
  323.  puts "  *** ERROR: unable to open '${botnet-nick}.botchk' for writing"
  324.  puts ""
  325.  exit
  326. }
  327. puts $fd "#! /bin/sh
  328. #
  329. # ${botnet-nick}.botchk (generated on [clock format [clock seconds] -format "%B %d, %Y @ %I:%M%p"])
  330. #
  331. # Generated by AutoBotchk 1.11
  332. # Copyright (C) 1999-2003 Jeff Fisher (guppy@eggheads.org)
  333. # Copyright (C) 2004-2024 Eggheads Development Team
  334. #
  335.  
  336. # change this to the directory you run your bot from:
  337. botdir=\"$dir\"
  338.  
  339. # change this to the name of your bot's script in that directory:
  340. botscript=\"$binary $mainconf\"
  341.  
  342. # change this to the nickname of your bot (capitalization COUNTS)
  343. botname=\"${botnet-nick}\"
  344.  
  345. # change this to the name of your bot's userfile (capitalization COUNTS)
  346. userfile=\"$userfile\"
  347.  
  348. # change this to the name of your bot's pidfile (capitalization COUNTS)
  349. pidfile=\"$pidfile\"
  350.  
  351. ########## you probably don't need to change anything below here ##########
  352.  
  353. cd \$botdir
  354.  
  355. # is there a pid file?
  356. if test -r \$pidfile
  357. then
  358.  # there is a pid file -- is it current?
  359.  botpid=`cat \$pidfile`
  360.  if `kill -CHLD \$botpid >/dev/null 2>&1`
  361.  then
  362.    # it's still going -- back out quietly
  363.    exit 0
  364.  fi
  365.  echo \"\"
  366.  echo \"Stale \$pidfile file, erasing...\"
  367.  rm -f \$pidfile
  368. fi
  369.  
  370. if test -r CANTSTART.\$botname
  371. then
  372.  if test -r \$userfile || test -r \$userfile~new || test -r \$userfile~bak
  373.  then
  374.    echo \"\"
  375.    echo \"Userfile found, removing check file 'CANTSTART.\$botname'...\"
  376.    rm -f CANTSTART.\$botname
  377.  fi
  378. fi
  379.  
  380. # test if we have run botchk previously and didn't find a userfile
  381. if test ! -f CANTSTART.\$botname
  382. then
  383.  echo \"\"
  384.  echo \"Couldn't find bot '\$botname' running, reloading...\"
  385.  echo \"\"
  386.  # check for userfile and reload bot if found
  387.  if test -r \$userfile
  388.  then
  389.    # It's there, load the bot
  390.    ./\$botscript
  391.    exit 0
  392.  else
  393.    if test -r \$userfile~new
  394.    then
  395.      # Bot f*@!ed up while saving the userfile last time.  Move it over.
  396.      echo \"Userfile missing.  Using last saved userfile...\"
  397.      mv -f \$userfile~new \$userfile
  398.      ./\$botscript
  399.      exit 0
  400.    else
  401.      if test -r \$userfile~bak
  402.      then
  403.        # Userfile is missing, use backup userfile.
  404.        echo \"Userfile missing.  Using backup userfile...\"
  405.        cp -f \$userfile~bak \$userfile
  406.        ./\$botscript
  407.        exit 0
  408.      else
  409.        # Well, nothing to work with...
  410.        echo \"No userfile.  Could not reload the bot...\"
  411.        echo \"no userfile\" > CANTSTART.\$botname
  412.        exit 1
  413.      fi
  414.    fi
  415.  fi
  416. fi
  417.  
  418. exit 0
  419. "
  420. close $fd
  421. puts "Wrote '${botnet-nick}.botchk' successfully ([file size $dir/${botnet-nick}.botchk] bytes)"
  422. if {[catch {exec chmod u+x $dir/${botnet-nick}.botchk} 0]} {
  423.  puts "  *** ERROR: unable to 'chmod u+x' the output file"
  424.  puts ""
  425.  exit
  426. }
  427. puts -nonewline "Scanning crontab entries ... "
  428.  
  429. set tmp ".autobotchk[clock clicks].[pid]"
  430. set cronbotchk [string map {\\ \\\\ \[ \\\[ \] \\\]} "${botnet-nick}.botchk"]
  431. if {$email} {
  432.   set line "$minutes \* \* \* \* $dir/${cronbotchk}"
  433. } {
  434.   set line "$minutes \* \* \* \* $dir/${cronbotchk} >\/dev\/null 2>&1"
  435. }
  436.  
  437. if {[catch {exec crontab -l > $tmp} error ]} {
  438.   if {![string match "*no*cron*" [string tolower $error]] &&
  439.       ![string match "*can't open*" [string tolower $error]]} {
  440.     catch {file delete -force $tmp} 0
  441.     puts "error: unable to get crontab listing"
  442.     puts ""
  443.     puts $error
  444.     puts ""
  445.     exit
  446.   }
  447. }
  448.  
  449. set cronbotchk [string map {\\ \\\\ \[ \\\[ \] \\\]} "${cronbotchk}"]
  450. set fd [open $tmp r]
  451. while {![eof $fd]} {
  452.   set z [gets $fd]
  453.   if {[string match "*$dir/${cronbotchk}*" $z] ||
  454.       [string match "*$dir//${cronbotchk}*" $z]} {
  455.     puts "found an existing entry, we're done now"
  456.     puts ""
  457.     exit
  458.   }
  459. }
  460.  
  461. close $fd
  462.  
  463. puts "done"
  464.  
  465. puts -nonewline "Adding the new crontab entry ... "
  466. set fd [open $tmp a]
  467. puts $fd $line
  468. close $fd
  469.  
  470. if {[catch {exec crontab $tmp} error]} {
  471.   puts "error: unable to do 'crontab $tmp'"
  472.   puts ""
  473.   puts $error
  474.   puts ""
  475.   exit
  476. } else {
  477.   catch {file delete -force $tmp} 0
  478. }
  479.  
  480. puts "done"
  481. puts ""
  482. puts "Use 'crontab -l' to view all your current crontab entries"
  483. puts "    'crontab -r' to remove all your crontab entries"
  484. puts ""
  485.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement