Ignore:
Timestamp:
Jan 28, 2006, 6:42:59 PM (13 years ago)
Author:
bcornec
Message:

monitas latest version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/monitas/server.c

    r352 r353  
    8080
    8181#define NOOF_THREADS 10
    82 #define LOGFILE "/var/log/monitas-server.log"
    83 #define log_it(x,y) { log_it_SUB(g_logfile,x,y); }
     82//#define LOGFILE "/var/log/monitas-server.log"
     83//#define log_it(x,y) { log_it_SUB(g_logfile,x,y); }
    8484
    8585
     
    100100extern char *tmsg_to_string(t_msg);
    101101extern int transmit_file_to_socket(FILE*,int);
    102 
     102extern int parse_options(int argc, char *argv[]);
    103103
    104104
     
    108108char g_command_fifo[MAX_STR_LEN+1]; // Device which server will monitor for incoming commands
    109109pthread_t g_threadinfo[NOOF_THREADS]; // One thread per port, to watch for requests from clients
    110 char g_logfile[MAX_STR_LEN+1] = "/var/log/monitas-server.log";
    111110char g_server_status_file[MAX_STR_LEN+1];
    112111
     
    156155  FILE*fout;
    157156
    158   sprintf(tmp, "%s - backup of %s commencing", ipaddr, clientpath);
    159   log_it(info, tmp);
    160   sprintf(outfile, "/var/spool/monitas/%s/%s.dat", ipaddr, call_program_and_get_last_line_of_output("date +%s"));
    161   if (does_file_exist(outfile)) { log_it(error, "Backup storage location exists already. That should be impossible."); return(1); }
     157  log_it(info, "%s - backup of %s commencing", ipaddr, clientpath);
     158  sprintf(outfile, "/var/spool/monitas/%s/%lu.dat", ipaddr, time(NULL));
     159  if (does_file_exist(outfile)) { log_it(error, "Backup storage location '%s' exists already. That should be impossible.", outfile); return(1); }
    162160  if (make_hole_for_file(outfile))
    163     { res++; log_it(error, "Cannot write archive to spool dir"); }
     161    { res++; log_it(error, "Cannot write archive to spool dir '%s'", outfile); }
    164162  else if (!(fout=fopen(outfile, "w")))
    165     { res++; log_it(fatal, "Failed to openout temp data file"); }
     163    { res++; log_it(fatal, "Failed to openout temp data file '%s'", outfile); }
    166164  else
    167165    {
    168       sprintf(tmp, "Backing up %s - archive=%s", ipaddr, outfile);
    169       log_it(debug, tmp);
     166      log_it(debug, "Backing up %s - archive=%s", ipaddr, outfile);
    170167      rec_to_client.msg_type = trigger_backup;
    171168      strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    193190  if (res>0)
    194191    {
    195       sprintf(tmp, "%s - error(s) occurred while backing up %s", ipaddr, clientpath);
    196       log_it(error, tmp);
     192      log_it(error, "%s - error(s) occurred while backing up %s", ipaddr, clientpath);
    197193      rec_to_client.msg_type = backup_fail;
    198194      sprintf(rec_to_client.body, "Failed to backup %s", clientpath);
     
    202198  else
    203199    {
    204       sprintf(tmp, "%s - backed up %s ok", ipaddr, clientpath);
    205       log_it(info, tmp);
     200      log_it(info, "%s - backed up %s ok", ipaddr, clientpath);
    206201      rec_to_client.msg_type = backup_ok;
    207202      sprintf(rec_to_client.body, "%s - backed up ok", clientpath);
     
    211206    {
    212207      res++;
    213       sprintf(tmp, "Unable to notify %s of backup success/failure", ipaddr);
    214       log_it(error, tmp);
     208      log_it(error, "Unable to notify %s of backup success/failure", ipaddr);
    215209      i = find_client_in_clientlist(ipaddr);
    216210      if (i>=0) { forcibly_logout_client(i); }
     
    241235  FILE*fin;
    242236
    243   sprintf(tmp, "%s - comparison of %s commencing", ipaddr, clientpath);
    244   log_it(info, tmp);
     237  log_it(info, "%s - comparison of %s commencing", ipaddr, clientpath);
    245238// FIXME - don't assume the latest backup contains the files we want ;)
    246239  sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr);
    247240  strcpy(infile, call_program_and_get_last_line_of_output(tmp));
    248   sprintf(tmp, "Comparing to data file '%s'", infile);
    249   log_it(debug, tmp);
    250   if (!does_file_exist(infile)) { log_it(error, "Backup not found. That should be impossible."); return(1); }
    251   sprintf(tmp, "Comparing %s - archive=%s", ipaddr, infile);
    252   log_it(debug, tmp);
     241  log_it(debug, "Comparing to data file '%s'", infile);
     242  if (!does_file_exist(infile)) { log_it(error, "Backup '%s' not found. That should be impossible.", infile); return(1); }
     243  log_it(debug, "Comparing %s - archive=%s", ipaddr, infile);
    253244  rec_to_client.msg_type = trigger_compare;
    254245  strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    256247    { log_it(error, "compare_client - failed to send msg to client"); return(1); }
    257248  if (!(fin=fopen(infile, "r")))
    258     { log_it(fatal, "Failed to openin temp data file"); }
     249    { log_it(fatal, "Failed to openin temp data file '%s'", infile); }
    259250  res += transmit_file_to_socket(fin, socket_fd);
    260251  len=read(socket_fd, (char*)&i, sizeof(i));
     
    267258  if (res>0)
    268259    {
    269       sprintf(tmp, "%s - error(s) occurred while comparing %s", ipaddr, clientpath);
    270       log_it(error, tmp);
     260      log_it(error, "%s - error(s) occurred while comparing %s", ipaddr, clientpath);
    271261      rec_to_client.msg_type = compare_fail;
    272262      sprintf(rec_to_client.body, "Failed to compare %s", clientpath);
     
    275265  else
    276266    {
    277       sprintf(tmp, "%s - compared %s ok", ipaddr, clientpath);
    278       log_it(info, tmp);
     267      log_it(info, "%s - compared %s ok", ipaddr, clientpath);
    279268      rec_to_client.msg_type = compare_ok;
    280269      sprintf(rec_to_client.body, "%s - compared ok", clientpath);
     
    283272  if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd))
    284273    {
    285       sprintf(tmp, "Unable to notify %s of compare success/failure", ipaddr);
    286       log_it(error, tmp);
     274      log_it(error, "Unable to notify %s of compare success/failure", ipaddr);
    287275      i = find_client_in_clientlist(ipaddr);
    288276      if (i>=0) { forcibly_logout_client(i); }
     
    307295{
    308296  int i;
    309   char tmp[MAX_STR_LEN+1];
    310297
    311298  for(i = 0; i < g_clientlist.items; i++)
     
    313300      if (!strcmp(clientIP, g_clientlist.el[i].ipaddr))
    314301    { return(i); }
    315       sprintf(tmp, "find_client_in_clientlist: Compared %s to clientlist[%d]=%s; failed\n", clientIP, i, g_clientlist.el[i].ipaddr);
    316       log_it(debug, tmp);
     302      log_it(debug, "find_client_in_clientlist: Compared %s to clientlist[%d]=%s; failed\n", clientIP, i, g_clientlist.el[i].ipaddr);
    317303    }
    318304  return(-1);
     
    359345  int res=0;
    360346
    361   sprintf(tmp, "Forcibly logging %s out", g_clientlist.el[clientno].ipaddr);
    362   log_it(info, tmp);
     347  log_it(info, "Forcibly logging %s out", g_clientlist.el[clientno].ipaddr);
    363348  rec_to_client.msg_type = logout_ok; /* to confirm logout */
    364349  strcpy(rec_to_client.body, "Server is shutting down. You are forced to logout");
     
    366351  if (--g_clientlist.items > 0)
    367352    {
    368       sprintf(tmp, "Moving clientlist[%d] to clientlist[%d]", clientno, g_clientlist.items);
    369       log_it(debug, tmp);
     353      log_it(debug, "Moving clientlist[%d] to clientlist[%d]", clientno, g_clientlist.items);
    370354      sprintf(tmp, "Was ipaddr=%s; now is ipaddr=", g_clientlist.el[clientno].ipaddr);
    371355      memcpy((void*)&g_clientlist.el[clientno], (void*)&g_clientlist.el[g_clientlist.items], sizeof(struct s_registered_client_record));
    372356      strcat(tmp, g_clientlist.el[clientno].ipaddr);
     357      /* FIXME: tmp must never contain '%'-sequences */
    373358      log_it(debug, tmp);
    374359    }
     
    419404*/
    420405{
    421   char clientIP[MAX_STR_LEN+1], tmp[MAX_STR_LEN+1];
     406  char clientIP[MAX_STR_LEN+1];
    422407  unsigned char *ptr;
    423408  int res=0;
     
    426411  ptr = (unsigned char*)(&sin->sin_addr);
    427412  sprintf(clientIP, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);
    428   sprintf(tmp, "clientIP = %s", clientIP);
    429   log_it(debug, tmp);
    430   sprintf(tmp, "%s message from %s [%s] (port %d)", tmsg_to_string(rec->msg_type), clientIP, rec->body, rec->port);
    431   log_it(debug, tmp);
     413  log_it(debug, "clientIP = %s", clientIP);
     414  log_it(debug, "%s message from %s [%s] (port %d)", tmsg_to_string(rec->msg_type), clientIP, rec->body, rec->port);
    432415  switch(rec->msg_type)
    433416    {
     
    471454  struct s_server2client_msg_record rec_to_client;
    472455  int clientno;
    473   char tmp[MAX_STR_LEN+1];
    474456
    475457//FIXME - lock g_clientlist[]
     
    479461      rec_to_client.msg_type = login_fail;
    480462      sprintf(rec_to_client.body, "Sorry, you're already logged in!");
    481       sprintf(tmp, "Ignoring login rq from %s: he's already logged in.", clientIP);
    482       log_it(error, tmp);
     463      log_it(error, "Ignoring login rq from %s: he's already logged in.", clientIP);
    483464/* FIXME - ping client (which will have a child watching for incoming
    484465packets by now - you didn't forget to do that, did you? :)) - to find out
     
    497478      g_clientlist.el[clientno].busy = false;
    498479      g_clientlist.items ++;
    499       sprintf(tmp, "Login request from %s ACCEPTED", clientIP);
    500       log_it(info, tmp);
     480      log_it(info, "Login request from %s ACCEPTED", clientIP);
    501481      strcpy(g_clientlist.el[clientno].last_progress_rpt, "Logged in");
    502482    }
     
    523503  struct s_server2client_msg_record rec_to_client;
    524504  int i, res=0;
    525   char tmp[MAX_STR_LEN+1];
    526505
    527506  i = find_client_in_clientlist(clientIP);
     
    550529      g_clientlist.items--;
    551530      rec_to_client.msg_type = logout_ok; /* to confirm logout */
    552       sprintf(tmp, "Logout request from %s ACCEPTED", clientIP);
    553       log_it(info, tmp);
     531      log_it(info,  "Logout request from %s ACCEPTED", clientIP);
    554532    }
    555533  send_msg_to_client(&rec_to_client, clientIP, rec_from_client->port, NULL);
     
    575553  struct s_server2client_msg_record rec_to_client;
    576554  int i;
    577   char tmp[MAX_STR_LEN+1];
    578555
    579556  i = find_client_in_clientlist(clientIP);
    580557  if (i < 0)
    581558    {
    582       sprintf(tmp, "Hey, %s isn't logged in. I'm not going to pong him.", clientIP);
    583       log_it(error, tmp);
     559      log_it(error, "Hey, %s isn't logged in. I'm not going to pong him.", clientIP);
    584560    }
    585561  else
     
    611587//  struct s_server2client_msg_record rec_to_client;
    612588  int i, res=0;
    613   char tmp[MAX_STR_LEN+1];
    614589
    615590  i = find_client_in_clientlist(clientIP);
    616591  if (i < 0)
    617592    {
    618       sprintf(tmp, "Hey, %s isn't logged in. I'm not going to deal with his progress_rpt.", clientIP);
    619       log_it(error, tmp);
     593      log_it(error, "Hey, %s isn't logged in. I'm not going to deal with his progress_rpt.", clientIP);
    620594      res++;
    621595    }
     
    645619//  struct s_server2client_msg_record rec_to_client;
    646620  int i, res=0;
    647   char tmp[MAX_STR_LEN+1], command[MAX_STR_LEN+1], first_half[MAX_STR_LEN+1], second_half[MAX_STR_LEN+1], *p;
     621  char command[MAX_STR_LEN+1], first_half[MAX_STR_LEN+1], second_half[MAX_STR_LEN+1], *p;
    648622
    649623  i = find_client_in_clientlist(clientIP);
    650624  if (i < 0)
    651625    {
    652       sprintf(tmp, "Hey, %s isn't logged in. I'm not going to deal with his request.", clientIP);
    653       log_it(error, tmp);
     626      log_it(error, "Hey, %s isn't logged in. I'm not going to deal with his request.", clientIP);
    654627      res++;
    655628    }
     
    659632      p = strchr(first_half, ' ');
    660633      if (!p) { second_half[0]='\0'; } else { strcpy(second_half, p); *p='\0'; }
    661       sprintf(command, "echo \"%s %s%s\" > %s", first_half, clientIP, second_half, SERVER_COMDEV);
     634      sprintf(command, "echo \"%s %s%s\" > %s", first_half, clientIP, second_half, g->server_comdev);
    662635      log_it(debug, command);
    663636      i = system(command);
     
    683656int res=0, port;
    684657int clientno;
    685 char tmp[MAX_STR_LEN+1];
    686658int pos;
    687659char command[MAX_STR_LEN+1], ipaddr[MAX_STR_LEN+1],
     
    698670
    699671//          for(i=0; i<strlen(command); i++) { command[i]=command[i]|0x60; }
    700           sprintf(tmp, "cmd=%s ipaddr=%s path=%s", command, ipaddr, path);
    701           log_it(debug, tmp);
    702           sprintf(tmp, "%s of %s on %s <-- command received", command, path, ipaddr);
    703           log_it(info, tmp);
     672          log_it(debug, "cmd=%s ipaddr=%s path=%s", command, ipaddr, path);
     673          log_it(info, "%s of %s on %s <-- command received", command, path, ipaddr);
    704674          if ((clientno = find_client_in_clientlist(ipaddr)) < 0)
    705675            {
    706               sprintf(tmp, "%s not found in clientlist; so, %s failed.", ipaddr, command);
    707               log_it(error, tmp);
     676              log_it(error, "%s not found in clientlist; so, %s failed.", ipaddr, command);
    708677            }
    709678          else if (g_clientlist.el[clientno].busy == true)
    710679            {
    711               sprintf(tmp, "%s is busy; so, %s failed.", ipaddr, command);
    712               log_it(error, tmp);
     680              log_it(error, "%s is busy; so, %s failed.", ipaddr, command);
    713681            }
    714682          else
     
    724692              else
    725693                {
    726                   sprintf(tmp, "%s - cannot '%s'. Command unknown.", ipaddr, command);
    727                   log_it(error, tmp);
     694                  log_it(error, "%s - cannot '%s'. Command unknown.", ipaddr, command);
    728695                  res=1;
    729696                }
     
    753720  FILE*fin;
    754721
    755   sprintf(tmp, "%s - restoration of %s commencing", ipaddr, clientpath);
    756   log_it(info, tmp);
     722  log_it(info, "%s - restoration of %s commencing", ipaddr, clientpath);
    757723// FIXME - don't assume the latest backup contains the files we want ;)
    758724  sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr);
    759725  strcpy(infile, call_program_and_get_last_line_of_output(tmp));
    760   sprintf(tmp, "Restoring from data file '%s'", infile);
    761   log_it(debug, tmp);
    762   if (!does_file_exist(infile)) { log_it(error, "Backup not found. That should be impossible."); return(1); }
    763   sprintf(tmp, "Restoring %s - archive=%s", ipaddr, infile);
    764   log_it(debug, tmp);
     726  log_it(debug, "Restoring from data file '%s'", infile);
     727  if (!does_file_exist(infile)) { log_it(error, "Backup '%s' not found. That should be impossible.", infile); return(1); }
     728  log_it(debug, "Restoring %s - archive=%s", ipaddr, infile);
    765729  rec_to_client.msg_type = trigger_restore;
    766730  strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    782746  if (res>0)
    783747    {
    784       sprintf(tmp, "%s - error(s) occurred while restoring %s", ipaddr, clientpath);
    785       log_it(error, tmp);
     748      log_it(error, "%s - error(s) occurred while restoring %s", ipaddr, clientpath);
    786749      rec_to_client.msg_type = restore_fail;
    787750      sprintf(rec_to_client.body, "Failed to restore %s", clientpath);
     
    790753  else
    791754    {
    792       sprintf(tmp, "%s - restored %s ok", ipaddr, clientpath);
    793       log_it(info, tmp);
     755      log_it(info, "%s - restored %s ok", ipaddr, clientpath);
    794756      rec_to_client.msg_type = restore_ok;
    795757      sprintf(rec_to_client.body, "%s - restored ok", clientpath);
     
    798760  if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd))
    799761    {
    800       sprintf(tmp, "Unable to notify %s of restore success/failure", ipaddr);
    801       log_it(error, tmp);
     762      log_it(error, "Unable to notify %s of restore success/failure", ipaddr);
    802763      i = find_client_in_clientlist(ipaddr);
    803764      if (i>=0) { forcibly_logout_client(i); }
     
    830791struct sockaddr_in sin;
    831792int  s;
    832 char tmp[MAX_STR_LEN+1];
    833793if ((hp = gethostbyname(clientIP)) == NULL)
    834794{
    835 sprintf(tmp, "send_msg_to_client: %s: unknown host", clientIP);
    836 log_it(error, tmp);
     795log_it(error, "send_msg_to_client: %s: unknown host", clientIP);
    837796return(1);
    838797}
     
    845804{ perror("socket"); log_it(error, "send_msg_to_client: SOCKET error"); return(1); }
    846805if (connect(s, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0)
    847 { sprintf(tmp, "Failed to connect to client %s on port %d", clientIP, port); log_it(error, tmp); return(1); }
     806{ log_it(error, "Failed to connect to client %s on port %d", clientIP, port); return(1); }
    848807send(s, (char*)rec, sizeof(struct s_server2client_msg_record), 0);
    849808if (psocket) { *psocket=s; } else { close(s); }
    850 sprintf(tmp, "Sent %s msg [%s] to %s (port %d)", tmsg_to_string(rec->msg_type), rec->body, clientIP, port);
    851 log_it(debug, tmp);
     809log_it(debug, "Sent %s msg [%s] to %s (port %d)", tmsg_to_string(rec->msg_type), rec->body, clientIP, port);
    852810return(0);
    853811}
     
    883841      usleep(50000);
    884842    }
    885   sprintf(tmp, "Now monitoring ports %d thru %d for requests from clients.", 8700, 8700+NOOF_THREADS-1);
    886   log_it(info, tmp);
     843  log_it(info, "Now monitoring ports %d thru %d for requests from clients.", 8700, 8700+NOOF_THREADS-1);
    887844}
    888845
     
    950907  int watch_port;
    951908  struct sockaddr_in sin;
    952   char buf[MAX_STR_LEN+1], tmp[MAX_STR_LEN+1];
     909  char buf[MAX_STR_LEN+1];
    953910  int len, s, new_s;
    954911  struct s_client2server_msg_record rec;
     
    962919  if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    963920    {
    964       sprintf(tmp, "Unable to open socket on port #%d", watch_port);
    965       log_it(error, tmp);
     921      log_it(error, "Unable to open socket on port #%d", watch_port);
    966922      return((void*)-1);
    967923    }
    968924  if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    969925    {
    970       sprintf(tmp, "Cannot bind %d - %s\n", watch_port, strerror(errno));
    971       log_it(error, tmp);
     926      log_it(error, "Cannot bind %d - %s\n", watch_port, strerror(errno));
    972927      return((void*)-1);
    973928    }
    974929  if (listen(s, MAX_PENDING) < 0)
    975930    {
    976       sprintf(tmp, "Cannot setup listen (%d) - %s\n", watch_port, strerror(errno));
    977       log_it(error, tmp);
     931      log_it(error, "Cannot setup listen (%d) - %s\n", watch_port, strerror(errno));
    978932      return((void*)-1);
    979933    }
    980934  /* service incoming connections */
    981   sprintf(tmp, "Bound port #%d OK", watch_port);
    982   log_it(debug, tmp);
     935  log_it(debug, "Bound port #%d OK", watch_port);
    983936  while(true)
    984937    {
     
    1016969  pthread_t server_status_thread;
    1017970
     971  parse_options(argc, argv);
     972
    1018973  log_it(info, "---------- Monitas (server) by Hugo Rabson ----------");
    1019974  register_pid(getpid(), "server");
    1020975  set_signals(true);
    1021976  start_threads_to_watch_ports_for_requests();
    1022   pthread_create(&server_status_thread, NULL, generate_server_status_file_regularly, (void*)SERVER_STATUS_FILE);
    1023   create_and_watch_fifo_for_commands(SERVER_COMDEV);
     977  pthread_create(&server_status_thread, NULL, generate_server_status_file_regularly, (void*)g->server_status_file);
     978  create_and_watch_fifo_for_commands(g->server_comdev);
    1024979  log_it(warn, "Execution should never reach this point");
    1025980  exit(0);
Note: See TracChangeset for help on using the changeset viewer.