📄 respmd.c
字号:
/*************************************************************************** respmd.c - description ------------------- begin : Tue May 28 14:13:57 EEST 2002 copyright : (C) 2002 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include<stdio.h>#include<sys/time.h>#include<sys/resource.h>#include<sys/stat.h>#include<sys/types.h>#include<string.h> // Include this for memset()#include<unistd.h>#include<fcntl.h>#include<stdlib.h>#include <sys/wait.h>#include <stdarg.h>#include <time.h> //for time#include "respmd.h"//#include <mcheck.h> //Memory DEBUG//internal subs//static void connect_alarm(int signo);//Global variablesunsigned char CRYPTIV[9];char CRYPTKEY[17];char SERVER_IP[30];char LOCALHOSTN[21];char SERVER_PORT[6]="8881";char PIDDIR[20]="/var/run";char my_host[64]="\0";int gethostn=0;int writetosyslog=0;int verbose=0;char *TIEDOSTO;struct smtps_t *head_smtp,*smtp_servers;void smtpfree( struct smtps_t *pointer ) { struct smtps_t *deletep; while(pointer) { deletep = pointer->next; free(pointer); pointer = deletep; } head_smtp=(struct smtps_t *) calloc(1,sizeof(struct smtps_t)); smtp_servers=head_smtp; head_smtp->next=NULL;}void sigpipe(){ slog(1,"Received SIGPIPE.");}void sighup(){ int ret=0; smtpfree(head_smtp); ret=read_confd(TIEDOSTO,smtp_servers); //read config file if(ret!=0) { slog(1,"respmd.c: Error while reading config file"); } slog(1,"Received SIGHUP.\n");}void sigusr1(){ struct smtps_t *smtppointer; smtppointer=head_smtp->next; if(smtppointer==NULL) { slog(1,"No server's in configuration."); sleep(2); } else { slog(1,"Status:"); while( smtppointer ) { if(smtppointer->up==1) slog(1,"%s %s is up.",smtppointer->serv,smtppointer->name); else slog(1,"%s %s is down.",smtppointer->serv,smtppointer->name); smtppointer = smtppointer->next; } } slog(1,"Received SIGUSR1.\n");}void sigterm(){ char error_msg2[80]="\0"; sprintf(error_msg2,"respmd: Received SIGTERM. Shutting down."); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "INFO", error_msg2, LOCALHOSTN, "NONE", "NONE", "INTERNAL") < 0) slog(2, "respmd.c: error while sending to server."); slog(1,"Received SIGTERM. Shutting down.\n"); exit(0);}//SERVERint server() //server sub program{ int ret=0, tmp_time=0, go=0; struct smtps_t *smtppointer; char error_msg[100]="\0"; time_t clock; while(1) //main loop { smtppointer=head_smtp->next; if(smtppointer==NULL) //if it is NULL no servers in config { slog(1,"No server's in configuration."); sleep(2); } else { while( smtppointer ) { if(smtppointer->polltime==0) //if polltime is 0 its set to default 300 sec (5 min) smtppointer->polltime=300; clock = time(NULL); tmp_time = (int)clock - (int)smtppointer->lastpoll; if(tmp_time>=smtppointer->polltime || (int)smtppointer->lastpoll==0) { if(strcmp(smtppointer->serv,"SMTP")==0) //If it is SMTP { ret = poll_smtp(smtppointer); go=1; } if(strcmp(smtppointer->serv,"HTTP")==0) //If it is HTTP { ret = poll_http(smtppointer); go=1; } if(strcmp(smtppointer->serv,"NNTP")==0) //If it is NNTP { ret = poll_nntp(smtppointer); go=1; } if(strcmp(smtppointer->serv,"IMAP")==0) //If it is IMAP { ret = poll_imap(smtppointer); go=1; } if(go!=1) //If no service is defined ignore server and send msg { sprintf(error_msg,"respmd: No service defined for: %s Ignoring server",smtppointer->name); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, LOCALHOSTN, "NONE", "NONE", "INTERNAL") < 0) slog(2, "servpd.c: error while sending to server."); go=0; } go=0; smtppointer->lastpoll = time(NULL); } if((int)smtppointer->lastpoll==0) //if lastpoll is 0 we set it to be now smtppointer->lastpoll = time(NULL); smtppointer = smtppointer->next; //move to next } } sleep(1); } //main loop ends return(0);} //server sub program ends//MAIN int main(int argc, char *argv[]) { // Main variables. // Process identification number to be returned from fork(). int process_id; int cc,ret=0; //Process the signals signal(SIGHUP, &sighup); signal(SIGTERM, &sigterm); signal(SIGUSR1, &sigusr1); signal(SIGPIPE, &sigpipe); if(argc==1) { show_menu(2); exit(1); } //mtrace(); //Memory leak DEBUG while((cc = getopt(argc, argv, "H:hV")) != EOF) { switch(cc) { case 'H': ret=strlen(optarg); if(ret>64) ret=63; strncpy(my_host,optarg,ret); ret=0; gethostn=1; break; case 'V': show_menu(1); exit(0); break; case 'h': show_menu(2); exit(0); break; default : show_menu(2); exit(0); } } TIEDOSTO = argv[optind]; if(TIEDOSTO == NULL) { printf("You didnt give me a config file, using default /etc/linux_mon.conf\n"); TIEDOSTO = "/etc/linux_mon.conf"; } printf("\nLinux Monitor: Service Response Monitor %s\n\n",VERSION); //print Banner //Lets initialize smtp struct. head_smtp=(struct smtps_t *) calloc(1,sizeof(struct smtps_t)); smtp_servers=head_smtp; head_smtp->next=NULL; ret=read_confd(TIEDOSTO,smtp_servers); //read config file if(gethostn==0) gethostname(my_host,sizeof(my_host)); //Lets gethostname we need this when polling smtp //Lets make the server run daemon process_id=make_deamon(); if(process_id<0) { printf("respmd.c: Error make_deamon failed!\n"); slog(2,"respmd.c: Error make_deamon failed\n"); return 1; } //run server part process_id=server(); if (process_id<0) { slog(2,"respmd.c: Error server failed to load\n"); return 1; } // End of code, obviously. return 0;}/* static void connect_alarm(int signo) { return; //just interrupt connect }*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -