#include "moni.h" /* * Functii */ /* * Functie care afiseaza in syslog eroare si incheie programul */ void errsys(char *message) { syslog(LOG_ERR, message); syslog(LOG_ERR, "EXITING !"); // printf("%s",message); closelog(); _exit(4); } /* * Functie care trimite un ping */ void send_ping(int s, HOST_ENTRY *h) { static char buffer[32]; struct icmp *icp = (struct icmp *) buffer; int n,len; // printf("din ping=%s si unslun=%lu \n",&h->host,hh->saddr.sin_addr.s_addr); gettimeofday( &h->last_time,&tz ); icp->icmp_type = ICMP_ECHO; icp->icmp_code = 0; icp->icmp_cksum = 0; icp->icmp_seq = h->in; icp->icmp_id = ident; memcpy(&buffer[SIZE_ICMP_HDR], &h->last_time, SIZE_LAST_TIME); memcpy(&buffer[SIZE_ICMP_HDR+SIZE_LAST_TIME], &h->num_packets_sent, SIZE_PACK_SENT); len = SIZE_ICMP_HDR+SIZE_LAST_TIME+SIZE_PACK_SENT; icp->icmp_cksum = in_cksum ( (u_short *)icp, len ); // // printf("in int ping=%lu\n",h->saddr.sin_addr.s_addr); n = sendto(s, buffer, len, 0, (struct sockaddr *)&h->saddr, sizeof(struct sockaddr_in)); // printf(); if ( n < 0 || n != len ) { syslog(LOG_WARNING, "Error while sending ping "); // printf("%s\n===%d","eroare la ping trimitere",n); // remove_job(h); } else { h->num_packets_sent++; } } int recvfrom_wto (int s, char *buf, int len, struct sockaddr *saddr, int timo) { int nfound,slen,n; struct timeval to; fd_set readset,writeset; to.tv_sec = timo/1000; to.tv_usec = (timo - (to.tv_sec*1000))*1000; FD_ZERO(&readset); FD_ZERO(&writeset); FD_SET(s,&readset); nfound = select(s+1,&readset,&writeset,NULL,&to); if (nfound<0) errsys("select"); if (nfound==0) return -1; /* timeout */ slen=sizeof(struct sockaddr); n=recvfrom(s,buf,len,0,saddr,&slen); if (n<0) errsys("recvfrom"); return n; } int wait_for_reply(HOST_ENTRY *h) { int result; static char buffer[4096]; struct sockaddr_in response_addr; struct ip *ip; int hlen; struct icmp *icp; int n; // HOST_ENTRY *h; long this_reply; int the_index; struct timeval sent_time; result=recvfrom_wto(s,buffer,4096,(struct sockaddr *)&response_addr,INTERVAL); // printf("ping from=%s\n",inet_ntoa(response_addr.sin_addr)); if (result<0) { return 0; } /* timeout */ ip = (struct ip *) buffer; hlen = ip->ip_hl << 2; if (result < hlen+ICMP_MINLEN) { return(2); /* too short */ } icp = (struct icmp *)(buffer + hlen); if ((icp->icmp_type != ICMP_ECHOREPLY) || (icp->icmp_id != ident)) { return 2; /* packet received, but not the one we are looking for! */ } /* if (( icp->icmp_seq >= num_hosts) || (!table[icp->icmp_seq]) || (table[icp->icmp_seq]->saddr.sin_addr.s_addr != response_addr.sin_addr.s_addr)) { return 2; packet already received, don't care about it anymore } */ if ((icp->icmp_seq != h->in) || (h->saddr.sin_addr.s_addr != response_addr.sin_addr.s_addr)) { return 2; } // n=icp->icmp_seq; // printf("secventa=%d\n",n); // h=table[n]; gettimeofday(&c_time,&tz); memcpy(&sent_time,&icp->icmp_data[0],sizeof(sent_time)); memcpy(&the_index,&icp->icmp_data[SIZE_LAST_TIME],sizeof(the_index)); this_reply = timeval_diff(&c_time,&sent_time); // process_client(h->host,1); // remove_job(h); /* remove job */ // return num_waiting; return 1; } char *strpu(char w[20]) { char w1[20],*ps,cc[2],*c,a[20]; strcpy(w1,w); strcpy(cc,"."); ps=w1; strtok(ps,"."); strcpy(a,ps); strcat(a,"_"); strcpy(w1,w); ps=w1; while(strncmp(ps,cc,1)) ps++;ps++; strtok(ps,"."); strcat(a,ps); strcat(a,"_"); strcpy(w1,w); ps=w1; while(strncmp(ps,cc,1)) ps++;ps++; while(strncmp(ps,cc,1)) ps++;ps++; strtok(ps,"."); strcat(a,ps); strcat(a,"_"); strcpy(w1,w); ps=w1; while(strncmp(ps,cc,1)) ps++;ps++; while(strncmp(ps,cc,1)) ps++;ps++; while(strncmp(ps,cc,1)) ps++;ps++; strtok(ps,"."); strcat(a,ps); ps=a; return ps; } int init_db() { int conectat; strcpy(db_host,"localhost"); strcpy(db_user,DB_USER); strcpy(db_password,DB_PASSWORD); strcpy(db_database,DB_DATABASE); int t; memset(&mysql,0,sizeof(MYSQL)); mysql_init(&mysql); mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"moni"); if (!mysql_real_connect(&mysql, db_host , db_user, db_password,db_database,0,NULL,0)) { // printf ("neconectare\n"); errsys("Failed to connect to database: Error:"); conectat = 0; } else { conectat = 1; if (mysql_select_db(&mysql, db_database)) { conectat=0; errsys("Nu pot selecta baza de date Error:"); } else { conectat = 1; } } return conectat; } void load_db() { struct in_addr *intmp; char *buff,*buf; if ((buff = ( char * ) malloc ( MAX_QUERY )) == NULL) errsys ( "buffer alloc" ); intmp=( struct in_addr * ) malloc ( sizeof(struct in_addr)); memset(buff, 0, MAX_QUERY); strcpy(buff,"SELECT hostip, hostnume, monitor from clienti "); init_db(); if (mysql_query(&mysql, buff)) { errsys("mysql_querry"); } if (!(res = mysql_store_result(&mysql))) { errsys("mysql_store_result"); } nr_host=0; while(row = mysql_fetch_row(res)) { if (strcmp(row[2],"1") == 0) { nr_host++; // printf("%s %s %s\n",row[0],row[1],row[2]); strcpy(hh[nr_host].host,row[0]); inet_aton(hh[nr_host].host,(struct in_addr *)&hh[nr_host].saddr.sin_addr.s_addr); // hh[nr_host].saddr.sin_addr.s_addr=&intmp->s_addr; // printf("inaddr=%lu %d\n",hh[nr_host].saddr.sin_addr.s_addr,nr_host); strcpy(hh[nr_host].table,strpu(hh[nr_host].host)); hh[nr_host].in = nr_host; hh[nr_host].stata = -1; hh[nr_host].stat = 0; } } mysql_free_result(res); mysql_close(&mysql); } void save_data() { char bf[100],bff[100]; int i; int max=40; init_db(); gettimeofday(&c_time, &tz); strftime(bff,max,"%Y%m%d%H%M%S",localtime(&c_time)); for(i=1;i<=nr_host;i++) { strcpy(bf,"insert into "); strcat(bf,hh[i].table); strcat(bf," (timet,stat) values( "); strcat(bf,bff); strcat(bf,","); if (hh[i].stat == 1) strcat(bf,"1)"); else strcat(bf,"0)"); //printf("xxx=%d\n",i); //printf("%s\n",bf); //printf("----%d----%d----",hh[i].stata,hh[i].stat); if (hh[i].stata != hh[i].stat) { if ( mysql_query(&mysql, bf) ) errsys("mysql_querry"); } } mysql_close(&mysql); } void verif() { int i; int st; // printf("\n%s\n","Trimit acu..."); for(i=1;i<=nr_host;i++) { // printf("din ver=%s",hh[i].host); send_ping(s,&hh[i]); st=wait_for_reply(&hh[i]); while (st>1) st=wait_for_reply(&hh[i]); hh[i].stata=hh[i].stat; hh[i].stat=st; // printf("sosire nr= %d\n",hh[i].stat); } } void sighup(int signal) { int i; load_db(); for(i=1;i<=nr_host;i++) { hh[i].stat=8; } } void sigterm(int signal) { int i; for(i=1;i<=nr_host;i++) { hh[i].stata=8; } save_data(); _exit(3); } int main(int argc, char *argv[]) { int i; long int j=0; char e[40],*pw; size_t max=20; struct tm *tmm; struct protoent *proto; if (getuid()) { errsys("You must be root to run this program !" ); _exit(1); } if ((proto = getprotobyname("icmp")) == NULL) errsys("icmp: unknown protocol"); s = socket( AF_INET, SOCK_RAW, proto->p_proto ); if ( s<0 ) errsys("can't create raw socket"); ident = getpid() & 0xFFFF; //syslog(LOG_INFO,"Started"); gettimeofday(&start_time, &tz); signal(SIGHUP,sighup); signal(SIGTERM,sigterm); // init_db(); // daemon if (fork()) _exit(0); setsid(); chdir("/"); umask(S_IRUSR); if (getuid()) { errsys( "You must be root to run this program !" ); _exit(1); } load_db(); for(i=1;i<=nr_host;i++) { hh[i].stat=8; } while(1) { verif(); save_data(); sleep(20); } }