Add nagircbot 0.0.20, IRC bot for Nagios.

PR:		ports/112210
Submitted by:	Antoine Beaupre <anarcat AT koumbit.org>
This commit is contained in:
Li-Wen Hsu 2007-07-06 07:15:08 +00:00
parent c119f2af3c
commit 430a488d97
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=195055
13 changed files with 378 additions and 0 deletions

View file

@ -104,6 +104,7 @@
SUBDIR += nagios12
SUBDIR += nagiosgraph
SUBDIR += nagiostat
SUBDIR += nagircbot
SUBDIR += nat
SUBDIR += nav
SUBDIR += nbtscan

View file

@ -0,0 +1,28 @@
# New ports collection makefile for: backupninja
# Date created: 7 june 2006
# Whom: anarcat
#
# $FreeBSD$
#
PORTNAME= nagircbot
PORTVERSION= 0.0.20
CATEGORIES= net-mgmt irc
MASTER_SITES= http://www.vanheusden.com/nagircbot/
EXTRACT_SUFX= .tgz
MAINTAINER= anarcat@anarcat.ath.cx
COMMENT= IRC bot for Nagios
USE_GMAKE= yes
USE_RC_SUBR= nagircbot
PLIST_FILES= bin/nagircbot
.include <bsd.port.pre.mk>
.if ${OSVERSION} < 600000
IGNORE= need at least FreeBSD-6
.endif
.include <bsd.port.post.mk>

View file

@ -0,0 +1,3 @@
MD5 (nagircbot-0.0.20.tgz) = c4692e5557d278b597cff0cec09dd0e0
SHA256 (nagircbot-0.0.20.tgz) = 22164ff2290c4bf2bebda60d5c09438f61e1973529d03a53bebd3bb36e43fc59
SIZE (nagircbot-0.0.20.tgz) = 17548

View file

@ -0,0 +1,31 @@
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: nagircbot
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable nagircbot:
#
# nagircbot_enable="YES"
#
# You must configure at least some flags to make it connect to a server. See -h.
#
. %%RC_SUBR%%
name=nagircbot
rcvar=`set_rcvar`
pidfile=/var/run/nagios/${name}.pid
command=%%PREFIX%%/bin/nagircbot
command_args="-P $pidfile -z nagios"
# set defaults
nagircbot_enable=${nagircbot_enable:-"NO"}
nagircbot_flags=${nagircbot_flags:-""}
load_rc_config ${name}
run_rc_command "$1"

View file

@ -0,0 +1,24 @@
--- Makefile.orig Mon Nov 27 06:21:58 2006
+++ Makefile Sat Apr 28 14:14:16 2007
@@ -1,9 +1,10 @@
VERSION=0.0.20
DEBUG= -g -D_DEBUG #-fprofile-arcs -ftest-coverage # -pg -g
-CXXFLAGS+=-Wall -O2 -DVERSION=\"${VERSION}\" $(DEBUG)
+CXXFLAGS+=-DUSE_MMAP -Wall -O2 -DVERSION=\"${VERSION}\" $(DEBUG)
CFLAGS+=${CXXFLAGS}
-LDFLAGS+=$(DEBUG) -lstdc++
+LDFLAGS+=$(DEBUG) -lstdc++ -lutil
+PREFIX?=/usr/local
OBJS=error.o log.o utils.o br.o pl.o anna.o
@@ -13,7 +14,7 @@
$(CC) -Wall -W $(OBJS) $(LDFLAGS) -o nagircbot
install: nagircbot
- cp nagircbot /usr/local/bin
+ cp nagircbot ${PREFIX}/bin
clean:
rm -f $(OBJS) nagircbot core *.da *.gcov *.bb*

View file

@ -0,0 +1,151 @@
--- anna.cpp.orig Mon Nov 27 06:21:58 2006
+++ anna.cpp Sat Apr 28 19:27:02 2007
@@ -12,6 +12,8 @@
#include <stdlib.h>
#include <signal.h>
#include <pwd.h>
+#include <sys/param.h>
+#include <libutil.h>
#include "utils.h"
#include "pl.h"
@@ -19,6 +21,7 @@
#include "error.h"
#include "log.h"
}
+#include "anna.h"
#define S_DISCONNECTED 1
#define S_CONNECTED 2
@@ -39,6 +42,7 @@
int minimum_time_for_successfull_login = 25; // one needs to be on-channel for at least 5 seconds to be considered a successfull login
int join_timeout = 5; // it should take no longer then 5 seconds to join a channel, otherwhise: abort connection and retry
int max_n_join_tries = 2; // try 2 times to get on a channel
+int throttle_delay = 1; // don't send more than one message per 1 seconds
char *server = "localhost:6667"; /* default irc server */
char *channel = "#nagircbot"; /* default channel to connect to */
char *nick = "nagircbot";
@@ -56,7 +60,7 @@
int max_time_last_host_update = 300, max_time_oldest_host_update = 3600, max_time_last_host_check = 300, max_time_oldest_host_check = 3 * 86400, max_time_last_service_check = 20 * 60, max_time_oldest_service_check = 3 * 86400, max_time_oldest_next_service_check = 20 * 60;
char *state_str[4] = { " OK ", "WARN", "CRIT", " ?? " };
-char *color_str[4] = { mystrdup("_3,1 "), mystrdup("_8,1 "), mystrdup("_4,1 "), mystrdup("_11,1 ") }; /* FIXME */
+char *color_str[4] = { mystrdup("_9,1 "), mystrdup("_8,1 "), mystrdup("_4,1 "), mystrdup("_11,1 ") }; /* FIXME */
struct stats *prev = NULL;
int n_prev = 0;
char topic[4096] = { 0 };
@@ -105,13 +109,18 @@
if (irc_set_nick(fd, nick) == -1)
return -1;
+ /* "Currently this requires that clients send a PASS command before sending
+ * the NICK/USER combination and servers *must* send a PASS command before
+ * any SERVER command." */
+ if (password != NULL) {
+ if (send_irc(fd, "PASS %s", password) == -1)
+ return -1;
+ }
+
/* FIXME: localhost must be, ehr, local host */
if (send_irc(fd, "USER %s \"localhost\" \"%s\" :%s", user, server, username) == -1)
return -1;
- if (password != NULL && send_irc(fd, "PASS %s", password) == -1)
- return -1;
-
return 0;
}
@@ -153,6 +162,12 @@
int irc_privmsg(int fd, char *channel, char *msg)
{
+ static time_t last_msg = time(NULL);
+ time_t diff = time(NULL) - last_msg;
+ if (diff < throttle_delay) {
+ sleep(throttle_delay - diff);
+ }
+ time(&last_msg);
return send_irc(fd, "PRIVMSG %s :%s", channel, msg);
}
@@ -166,7 +181,7 @@
/* open file or connection to nagios status socket */
if (is_file == 1) /* file */
- fd = open64(statuslog, O_RDONLY);
+ fd = open(statuslog, O_RDONLY);
else
fd = connect_to(statuslog);
if (fd == -1)
@@ -416,7 +431,7 @@
if (verbose > 1) dolog("reload_statuslog started");
if (statuslog_location == L_FILE) /* file */
- fd_sl = open64(statuslog, O_RDONLY);
+ fd_sl = open(statuslog, O_RDONLY);
else
fd_sl = connect_to(statuslog);
@@ -712,6 +727,7 @@
printf("-z user user to run as\n");
printf("-H show only state type 'HARD' (default)\n");
printf("-S show also state type 'SOFT'\n");
+ printf("-P file store the pid in a file\n");
}
int main(int argc, char *argv[])
@@ -724,14 +740,19 @@
time_t time_join_channel_started = (time_t)0;
time_t time_tcp_connected = (time_t)0;
int join_tries = 0;
- char *runas = NULL;
+ char *runas = NULL, *pidfile = NULL;
+ pid_t otherpid;
+
color_str[0][0] = color_str[1][0] = color_str[2][0] = color_str[3][0] = 3;
- while((c = getopt(argc, argv, "xXF:f:i:hHSs:c:Ctn:u:U:p:T:mvdVz:")) != -1)
+ while((c = getopt(argc, argv, "xXP:F:f:i:hHSs:c:Ctn:u:U:p:T:mvdVz:")) != -1)
{
switch(c)
{
+ case 'P':
+ pidfile = optarg;
+ break;
case 'z':
runas = optarg;
break;
@@ -867,6 +888,14 @@
}
}
+ pfh = pidfile_open(pidfile, 0600, &otherpid);
+ if (pfh == NULL) {
+ if (errno == EEXIST)
+ error_exit("Daemon already running, pid: %d.", otherpid);
+ /* If we cannot create pidfile from other reasons, only warn. */
+ dolog("Cannot open or create pidfile");
+ }
+
if (do_fork)
{
if (daemon(0, 0) == -1)
@@ -875,6 +904,9 @@
}
}
+ pidfile_write(pfh);
+
+
signal(SIGPIPE, SIG_IGN);
for(;;)
@@ -1056,5 +1088,6 @@
}
}
+ pidfile_remove(pfh);
return 0;
}

View file

@ -0,0 +1,4 @@
--- /dev/null Sat Apr 28 14:15:01 2007
+++ anna.h Sat Apr 28 14:16:47 2007
@@ -0,0 +1 @@
+struct pidfh *pfh;

View file

@ -0,0 +1,63 @@
--- br.cpp.orig Mon Nov 27 06:21:58 2006
+++ br.cpp Sat Apr 28 13:45:23 2007
@@ -29,11 +29,12 @@
#include <fcntl.h>
#include <syslog.h>
#include "br.h"
+#include "utils.h"
buffered_reader::buffered_reader(int cur_fd, int cur_block_size)
{
#ifdef USE_MMAP
- struct stat64 finfo;
+ struct stat finfo;
#endif
fd = cur_fd;
@@ -44,20 +45,20 @@
/* try do mmap */
#ifdef USE_MMAP
- if (fstat64(cur_fd, &finfo) == 0)
+ if (fstat(cur_fd, &finfo) == 0)
{
if (!S_ISFIFO(finfo.st_mode))
{
/* mmap */
size_of_file = finfo.st_size;
- cur_offset = mmap_addr = (char *)mmap64(NULL, size_of_file, PROT_READ, MAP_SHARED, cur_fd, 0);
+ cur_offset = mmap_addr = (char *)mmap(NULL, size_of_file, PROT_READ, MAP_SHARED, cur_fd, 0);
if (!mmap_addr)
{
- fprintf(stderr, "mmap64 failed: %d/%s\n", errno, strerror(errno));
+ fprintf(stderr, "mmap failed: %d/%s\n", errno, strerror(errno));
}
/* advise the kernel how to treat the mmaped region */
- /* FIXME: change to madvise64 as soon as it comes available */
+ /* FIXME: change to madvise as soon as it comes available */
(void)madvise(mmap_addr, size_of_file, MADV_SEQUENTIAL);
// fprintf(stderr, "*using mmap*\n");
@@ -237,7 +238,7 @@
n_bytes = lf_offset - buffer_pointer;
- out = strndup(&buffer[buffer_pointer], n_bytes);
+ out = mystrndup(&buffer[buffer_pointer], n_bytes);
if (!out)
{
fprintf(stderr, "buffered_reader::read_line: malloc(%lld) failed\n", n_bytes + 1);
@@ -251,10 +252,10 @@
return out;
}
-off64_t buffered_reader::file_offset(void)
+off_t buffered_reader::file_offset(void)
{
if (mmap_addr)
return cur_offset - mmap_addr;
else
- return lseek64(fd, 0, SEEK_CUR);
+ return lseek(fd, 0, SEEK_CUR);
}

View file

@ -0,0 +1,27 @@
--- br.h.orig Mon Nov 27 06:21:58 2006
+++ br.h Sat Apr 28 13:45:23 2007
@@ -16,6 +16,8 @@
*
*/
+#include <sys/types.h>
+
/* code taken from linux kernel */
#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
#define __builtin_expect(x, expected_value) (x)
@@ -33,7 +35,7 @@
char *buffer;
long long int buffer_length, buffer_pointer;
char *mmap_addr, *cur_offset;
- off64_t size_of_file;
+ off_t size_of_file;
int number_of_bytes_in_buffer(void);
int read_into_buffer(void);
@@ -46,5 +48,5 @@
char * read_line(void);
- off64_t file_offset(void);
+ off_t file_offset(void);
};

View file

@ -0,0 +1,20 @@
--- error.c.orig Sat Apr 28 14:08:54 2007
+++ error.c Sat Apr 28 14:17:12 2007
@@ -5,6 +5,9 @@
#include <sys/types.h>
#include <signal.h>
#include <syslog.h>
+#include <sys/param.h>
+#include <libutil.h>
+#include "anna.h"
void error_exit(char *format, ...)
{
@@ -17,6 +20,7 @@
fprintf(stderr, "%s: errno=%d (if applicable)\n", buffer, errno);
syslog(LOG_ERR, "'%s': %m", buffer);
+ pidfile_remove(pfh);
exit(EXIT_FAILURE);
}

View file

@ -0,0 +1,10 @@
--- utils.cpp.orig Mon Nov 27 06:21:58 2006
+++ utils.cpp Sat Apr 28 13:45:23 2007
@@ -29,6 +29,7 @@
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <netinet/in.h>
extern "C" {
#include "error.h"
}

View file

@ -0,0 +1,11 @@
--- utils.h.orig Mon Nov 27 06:21:58 2006
+++ utils.h Sat Apr 28 13:45:23 2007
@@ -19,7 +19,7 @@
void * mymalloc(int size, char *what);
void * myrealloc(void *oldp, int newsize, char *what);
char * mystrdup(char *in);
-char * mtstrndup(char *in, int len);
+char * mystrndup(char *in, int len);
void myfree(void *p, char *what);
ssize_t WRITE(int fd, char *whereto, size_t len);
int get_filesize(char *filename);

View file

@ -0,0 +1,5 @@
Nagircbot is an IRC bot that monitors Nagios's status file for changes and
announces those in an IRC channel. It can also change the topic of the channel
to reflect current status.
WWW: http://www.vanheusden.com/nagircbot/