Lines 1-75
Link Here
|
1 |
--- anna.cpp.orig Mon Nov 27 06:21:58 2006 |
1 |
--- anna.cpp.orig 2011-01-18 05:39:10.000000000 -0500 |
2 |
+++ anna.cpp Sat Apr 28 19:27:02 2007 |
2 |
+++ anna.cpp 2011-05-31 22:45:11.000000000 -0400 |
3 |
@@ -12,6 +12,8 @@ |
3 |
@@ -23,6 +23,7 @@ |
4 |
#include <stdlib.h> |
4 |
#include "ssl.h" |
5 |
#include <signal.h> |
|
|
6 |
#include <pwd.h> |
7 |
+#include <sys/param.h> |
8 |
+#include <libutil.h> |
9 |
|
10 |
#include "utils.h" |
11 |
#include "pl.h" |
12 |
@@ -19,6 +21,7 @@ |
13 |
#include "error.h" |
14 |
#include "log.h" |
5 |
#include "log.h" |
15 |
} |
6 |
} |
16 |
+#include "anna.h" |
7 |
+#include "anna.h" |
17 |
|
8 |
|
18 |
#define S_DISCONNECTED 1 |
9 |
#define S_DISCONNECTED 1 |
19 |
#define S_CONNECTED 2 |
10 |
#define S_CONNECTED 2 |
20 |
@@ -39,6 +42,7 @@ |
11 |
@@ -43,6 +44,7 @@ |
21 |
int minimum_time_for_successfull_login = 25; // one needs to be on-channel for at least 5 seconds to be considered a successfull login |
12 |
int minimum_time_for_successfull_login = 25; // one needs to be on-channel for at least 5 seconds to be considered a successfull login |
22 |
int join_timeout = 5; // it should take no longer then 5 seconds to join a channel, otherwhise: abort connection and retry |
13 |
int join_timeout = 5; // it should take no longer then 5 seconds to join a channel, otherwhise: abort connection and retry |
23 |
int max_n_join_tries = 2; // try 2 times to get on a channel |
14 |
int max_n_join_tries = 2; // try 2 times to get on a channel |
24 |
+int throttle_delay = 1; // don't send more than one message per 1 seconds |
15 |
+int throttle_delay = 1; // don't send more than one message per 1 seconds |
25 |
char *server = "localhost:6667"; /* default irc server */ |
16 |
char *server = "localhost:6667"; /* default irc server */ |
26 |
char *channel = "#nagircbot"; /* default channel to connect to */ |
17 |
char *channel = "#nagircbot"; /* default channel to connect to */ |
27 |
char *nick = "nagircbot"; |
18 |
char *nick_prefix = ""; /* prefix text for all messages sent to channel */ |
28 |
@@ -56,7 +60,7 @@ |
19 |
@@ -53,7 +55,7 @@ |
29 |
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; |
20 |
int one_line = 1; |
30 |
|
21 |
char *username = "Nagios IRC Bot " VERSION ", (C) www.vanheusden.com"; /* complete username */ |
31 |
char *state_str[4] = { " OK ", "WARN", "CRIT", " ?? " }; |
22 |
int verbose = 255; /* default is log everything */ |
32 |
-char *color_str[4] = { mystrdup("_3,1 "), mystrdup("_8,1 "), mystrdup("_4,1 "), mystrdup("_11,1 ") }; /* FIXME */ |
23 |
-char *statuslog = "/usr/local/nagios/var/status.log"; |
33 |
+char *color_str[4] = { mystrdup("_9,1 "), mystrdup("_8,1 "), mystrdup("_4,1 "), mystrdup("_11,1 ") }; /* FIXME */ |
24 |
+char *statuslog = "/var/spool/nagios/status.dat"; |
34 |
struct stats *prev = NULL; |
25 |
int statuslog_version = 2; |
35 |
int n_prev = 0; |
26 |
int statuslog_location = L_FILE; |
36 |
char topic[4096] = { 0 }; |
27 |
char use_colors = 0; |
37 |
@@ -105,13 +109,18 @@ |
28 |
@@ -174,6 +176,13 @@ |
38 |
if (irc_set_nick(fd, nick) == -1) |
|
|
39 |
return -1; |
40 |
|
41 |
+ /* "Currently this requires that clients send a PASS command before sending |
42 |
+ * the NICK/USER combination and servers *must* send a PASS command before |
43 |
+ * any SERVER command." */ |
44 |
+ if (password != NULL) { |
45 |
+ if (send_irc(fd, "PASS %s", password) == -1) |
46 |
+ return -1; |
47 |
+ } |
48 |
+ |
49 |
/* FIXME: localhost must be, ehr, local host */ |
50 |
if (send_irc(fd, "USER %s \"localhost\" \"%s\" :%s", user, server, username) == -1) |
51 |
return -1; |
52 |
|
53 |
- if (password != NULL && send_irc(fd, "PASS %s", password) == -1) |
54 |
- return -1; |
55 |
- |
56 |
return 0; |
57 |
} |
58 |
|
29 |
|
59 |
@@ -153,6 +162,12 @@ |
30 |
int irc_privmsg(server_t server_conn, char *channel, char *msg) |
60 |
|
|
|
61 |
int irc_privmsg(int fd, char *channel, char *msg) |
62 |
{ |
31 |
{ |
63 |
+ static time_t last_msg = time(NULL); |
32 |
+ static time_t last_msg = time(NULL); |
64 |
+ time_t diff = time(NULL) - last_msg; |
33 |
+ time_t diff = time(NULL) - last_msg; |
65 |
+ if (diff < throttle_delay) { |
34 |
+ if (diff < throttle_delay) { |
66 |
+ sleep(throttle_delay - diff); |
35 |
+ sleep(throttle_delay - diff); |
67 |
+ } |
36 |
+ } |
68 |
+ time(&last_msg); |
37 |
+ time(&last_msg); |
69 |
return send_irc(fd, "PRIVMSG %s :%s", channel, msg); |
38 |
+ |
|
|
39 |
return send_irc(server_conn, "PRIVMSG %s :%s", channel, msg); |
70 |
} |
40 |
} |
71 |
|
41 |
|
72 |
@@ -166,7 +181,7 @@ |
42 |
@@ -192,7 +201,7 @@ |
73 |
|
43 |
|
74 |
/* open file or connection to nagios status socket */ |
44 |
/* open file or connection to nagios status socket */ |
75 |
if (is_file == 1) /* file */ |
45 |
if (is_file == 1) /* file */ |
Lines 78-84
Link Here
|
78 |
else |
48 |
else |
79 |
fd = connect_to(statuslog); |
49 |
fd = connect_to(statuslog); |
80 |
if (fd == -1) |
50 |
if (fd == -1) |
81 |
@@ -416,7 +431,7 @@ |
51 |
@@ -490,7 +499,7 @@ |
82 |
if (verbose > 1) dolog("reload_statuslog started"); |
52 |
if (verbose > 1) dolog("reload_statuslog started"); |
83 |
|
53 |
|
84 |
if (statuslog_location == L_FILE) /* file */ |
54 |
if (statuslog_location == L_FILE) /* file */ |
Lines 87-151
Link Here
|
87 |
else |
57 |
else |
88 |
fd_sl = connect_to(statuslog); |
58 |
fd_sl = connect_to(statuslog); |
89 |
|
59 |
|
90 |
@@ -712,6 +727,7 @@ |
|
|
91 |
printf("-z user user to run as\n"); |
92 |
printf("-H show only state type 'HARD' (default)\n"); |
93 |
printf("-S show also state type 'SOFT'\n"); |
94 |
+ printf("-P file store the pid in a file\n"); |
95 |
} |
96 |
|
97 |
int main(int argc, char *argv[]) |
98 |
@@ -724,14 +740,19 @@ |
99 |
time_t time_join_channel_started = (time_t)0; |
100 |
time_t time_tcp_connected = (time_t)0; |
101 |
int join_tries = 0; |
102 |
- char *runas = NULL; |
103 |
+ char *runas = NULL, *pidfile = NULL; |
104 |
+ pid_t otherpid; |
105 |
+ |
106 |
|
107 |
color_str[0][0] = color_str[1][0] = color_str[2][0] = color_str[3][0] = 3; |
108 |
|
109 |
- while((c = getopt(argc, argv, "xXF:f:i:hHSs:c:Ctn:u:U:p:T:mvdVz:")) != -1) |
110 |
+ while((c = getopt(argc, argv, "xXP:F:f:i:hHSs:c:Ctn:u:U:p:T:mvdVz:")) != -1) |
111 |
{ |
112 |
switch(c) |
113 |
{ |
114 |
+ case 'P': |
115 |
+ pidfile = optarg; |
116 |
+ break; |
117 |
case 'z': |
118 |
runas = optarg; |
119 |
break; |
120 |
@@ -867,6 +888,14 @@ |
121 |
} |
122 |
} |
123 |
|
124 |
+ pfh = pidfile_open(pidfile, 0600, &otherpid); |
125 |
+ if (pfh == NULL) { |
126 |
+ if (errno == EEXIST) |
127 |
+ error_exit("Daemon already running, pid: %d.", otherpid); |
128 |
+ /* If we cannot create pidfile from other reasons, only warn. */ |
129 |
+ dolog("Cannot open or create pidfile"); |
130 |
+ } |
131 |
+ |
132 |
if (do_fork) |
133 |
{ |
134 |
if (daemon(0, 0) == -1) |
135 |
@@ -875,6 +904,9 @@ |
136 |
} |
137 |
} |
138 |
|
139 |
+ pidfile_write(pfh); |
140 |
+ |
141 |
+ |
142 |
signal(SIGPIPE, SIG_IGN); |
143 |
|
144 |
for(;;) |
145 |
@@ -1056,5 +1088,6 @@ |
146 |
} |
147 |
} |
148 |
|
149 |
+ pidfile_remove(pfh); |
150 |
return 0; |
151 |
} |