On Tue, Aug 16, 2005 at 10:09:49AM -0300, Felipe Mesquita wrote:
> in-kernel pppoe. Anyway, I've made the script that checks for the
> device ip, and Croned it every 3 minutes (Depending on the needs
> it can be higher..)
I took the smarts of your script, and also wrote a simple logging
tool. It's just a wrapper for the syslog(3) function call (that
allows you to log to syslog via the shell).
In root's crontab, I have this entry:
* * * * * /usr/local/sbin/chkpppoe.sh
The chkpppoe.sh script looks like this:
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
SYSLOGGER=/usr/local/sbin/syslogger
IFACE=pppoe0
PROGNAME=chkpppoe.sh
VAR=`ifconfig ${IFACE} | grep "inet " | cut -c 7`
if [ $VAR = "0" ];
then
${SYSLOGGER} "${PROGNAME}: ${IFACE} appears to be down; attempting to
bring it back up"
ifconfig ${IFACE} up
sleep 60
else
${SYSLOGGER} "${PROGNAME}: ${IFACE} appears to be up; doing nothing"
fi
#end of chkpppoe.sh
The 'syslogger' program is this trivial program:
/* syslogger.c begin */
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_PRIORITY LOG_INFO
#define DEFAULT_PRIORITY_STRING "LOG_INFO"
void usage (int argc, char* argv[])
{
printf ("usage: %s [-ln] <\"user message\">\n",
argv[0]);
printf ("where -ln specifies the log level, an n is is one of\n"
"\t-l0 LOG_EMERG\n"
"\t-l1 LOG_ALERT\n"
"\t-l2 LOG_CRIT\n"
"\t-l3 LOG_ERR\n"
"\t-l4 LOG_WARNING\n"
"\t-l5 LOG_NOTICE\n"
"\t-l6 LOG_INFO (default)\n"
"\t-l7 LOG_DEBUG\n"
);
exit(EXIT_SUCCESS);
}
int main (int argc, char* argv[])
{
int priority = DEFAULT_PRIORITY;
char* msg = NULL;
switch (argc) {
case 3:
if ( (3==strlen(argv[1])) &&
(0==strncmp("-l", argv[1], 2)) ) {
priority = (int)strtol(&argv[1][2],
(char**)NULL, 10);
if ( (priority < 0) || (priority > 7) ) {
fprintf(stderr,
"warning: no such
priority: %i; defaulting to "
"%s\n", priority,
DEFAULT_PRIORITY_STRING);
priority = DEFAULT_PRIORITY;
}
}
else {
fprintf(stderr,
"warning: invalid option '%s';
defaulting to "
"%s\n", argv[1],
DEFAULT_PRIORITY_STRING);
priority = DEFAULT_PRIORITY;
}
msg = argv[2];
break;
case 2:
msg = argv[1];
break;
default:
usage(argc,argv);
}
syslog(priority, "%s", msg);
return 0;
}
/* syslogger.c end */
I just saw the program actually work, and can verify it in
/var/log/messages:
...
Aug 23 09:14:01 excrement syslogger: chkpppoe.sh: pppoe0 appears to be up;
doing nothing
Aug 23 09:14:08 excrement /bsd: pppoe0: down
Aug 23 09:14:08 excrement /bsd: pppoe0: phase terminate
Aug 23 09:14:16 excrement /bsd: pppoe0: phase dead
Aug 23 09:15:01 excrement syslogger: chkpppoe.sh: pppoe0 appears to be down;
attempting to bring it back up
Aug 23 09:15:01 excrement /bsd: pppoe0: phase establish
Aug 23 09:15:01 excrement /bsd: pppoe0: phase authenticate
Aug 23 09:15:03 excrement /bsd: pppoe0: phase network
Aug 23 09:16:01 excrement syslogger: chkpppoe.sh: pppoe0 appears to be up;
doing nothing
...
Hopefully someone finds this useful :)
Matt
--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email