/* function to check if (pagerid) wants to be notified of server (hostname) returns 1 if they do, 0 if they do not, -1 on error */ int wants_notification(char *hostname, int pagerid) { MYSQL *mysqlConnection; MYSQL_RES *res; MYSQL_ROW row; char qry[1024]; int ql; my_ulonglong num_rows; char *ids, *tmp; int rv = 0; mysqlConnection = init_sql(); if (!mysqlConnection) return -1; sprintf(qry, "SELECT contactHosts.contactids FROM contactHosts,servers WHERE contactHosts.sid = servers.id AND servers.host='%s'", hostname); ql = strlen(qry); if (mysql_real_query(mysqlConnection, qry, ql)) return -1; if (! (res = mysql_store_result(mysqlConnection)) ) return -1; num_rows = mysql_num_rows(res); if (num_rows) { row = mysql_fetch_row(res); ids = strdup(row[0]); tmp = strtok(ids, ","); if (tmp) { while (1) { if (atoi(tmp) == pagerid) { rv = 1; break; } if (!(tmp = strtok(NULL, ","))) break; } } free(ids); } else { // we will assume if they don't have a record that they want to be notified rv = 1; } mysql_free_result(res); close_sql(mysqlConnection); return rv; }
static int h_log(int argc, unsigned char ** argv){ conf.logfunc = logstdout; if(conf.logtarget){ myfree(conf.logtarget); conf.logtarget = NULL; } if(argc > 1) { conf.logtarget = (unsigned char *)mystrdup((char *)argv[1]); if(*argv[1]=='@'){ #ifndef _WIN32 openlog((char *)conf.logtarget+1, LOG_PID, LOG_DAEMON); conf.logfunc = logsyslog; #endif } #ifndef NOODBC else if(*argv[1]=='&'){ pthread_mutex_lock(&log_mutex); close_sql(); init_sql((char *)argv[1]+1); pthread_mutex_unlock(&log_mutex); conf.logfunc = logsql; } #endif else { FILE *fp; if(argc > 2) { conf.logtype = getrotate(*argv[2]); } conf.logtime = time(0); if(conf.logname)myfree(conf.logname); conf.logname = (unsigned char *)mystrdup((char *)argv[1]); fp = fopen((char *)dologname (tmpbuf, conf.logname, NULL, conf.logtype, conf.logtime), "a"); if(!fp){ perror("fopen()"); return 1; } else { pthread_mutex_lock(&log_mutex); if(conf.stdlog)fclose(conf.stdlog); conf.stdlog = fp; pthread_mutex_unlock(&log_mutex); #ifdef _WINCE freopen(tmpbuf, "w", stdout); freopen(tmpbuf, "w", stderr); #endif } } } return 0; }
/* send notifcations out to all people(s) state is the 1 (up) or 0 (down), -1 error */ void send_notifications(char *hostname, int port, int state) { char buffer[255]; MYSQL *mysqlConnection; MYSQL_RES *res; MYSQL_ROW row; char qry[1024], buf[128]; int ql; mysqlConnection = init_sql(); if (!mysqlConnection) return; sprintf(buf, "%s %d %s", hostname, port, ((state == 1) ? "up" : "down") ); strcpy(qry, "SELECT pager,send_page,email,send_email,id FROM notifications"); ql = strlen(qry); if (mysql_real_query(mysqlConnection, qry, ql)) return; if (! (res = mysql_store_result(mysqlConnection)) ) return; while ((row = mysql_fetch_row(res))) { if (wants_notification(hostname, atoi(row[4]))) { #ifdef ARCH_HTTP_PAGE if (strcmp(row[1], "1") == 0) { /* send page */ /* check if they want to receive pages for this server */ sprintf(buffer, " sending page to %s...\n", row[0]); _log(buffer, 1); send_page(row[0], buf); } #endif if (strcmp(row[3], "1") == 0) { /* send e-mail */ sprintf(buffer, " sending email to %s...\n", row[2]); _log(buffer, 1); send_email(row[2], buf); } } } mysql_free_result(res); close_sql(mysqlConnection); }