/***************************************************************************
 *
 *           Copyright 1997 by Carnegie Mellon University
 * 
 *                       All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of CMU not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 * 
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>

#ifdef HAVE_STRINGS_H
# include <strings.h>
#else /* HAVE_STRINGS_H */
# include <string.h>
#endif /* HAVE_STRINGS_H */

#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif /* HAVE_MALLOC_H */

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /* HAVE_STDLIB_H */

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <snmp/snmp.h>

#include "version.h"

static char rcsid[] = 
"$Id: ctemp.c,v 1.1.1.1 1998/03/08 16:53:55 ryan Exp $";

/**********************************************************************/

void Usage(char *Err)
{
  if (Err != NULL)
    fprintf(stderr, "\nUnknown argument '%s'\n", Err);

  fprintf(stderr, "\nusage: ctemp [--version] [--config=/path/to/file]\n");

  fprintf(stderr, "--version:  Print version information.\n");
  fprintf(stderr, "--config=:  Config file to use.\n\n");
  fprintf(stderr, "The config file used is either:\n");
  fprintf(stderr, "\t the specified file\n");
  fprintf(stderr, "\t the file specified by CTEMP_CONF\n");
  fprintf(stderr, "\t ./routers.txt\n");
  fprintf(stderr, "\t %s\n", DEF_CONF);
  fprintf(stderr, "\n");
  exit(-1);
}

void Version(void)
{
  fprintf(stderr, "Cisco Temperatures, version %s\n", ctemp_Version());
  fprintf(stderr, "CMU SNMP Library Version %s\n", snmp_Version());
  exit(-1);
}

/**********************************************************************/

struct Device {
  char hostname[48];
  char ipaddr[48];
  char community[64];
  struct Device *Next;
};

struct Device *Head = NULL;

int readconf(char *File)
{
  FILE *config = NULL;
  char line[1024];
  struct Device *Ptr;
  int Num = 0;

  if (File)
    config = fopen(File, "r");

  if (config == NULL) {
    char *env;
    env = getenv("CTEMP_CONF");
    if (env)
      config = fopen(env, "r");
  }

  if (config == NULL)
    config = fopen("routers.txt", "r");

  if (config == NULL)
    config = fopen(DEF_CONF, "r");

  if (config == NULL) {
    fprintf(stderr, "Unable to locate configuration file!\n");
    exit(-1);
  }

  while (!(feof(config))) {

    /* First, read the line */
    if (fgets((char *)line, 1024, config) == NULL)
      break;

    /* Is this a comment? */
    if ((line[0] == '#') || 
	(line[0] == ';') ||
	(strlen((char *)line) < 6)) {
      /* A few blank spaces should actually be ignored.  I hope. */
      ;
	} else {

	  Ptr = (struct Device *)malloc(sizeof(struct Device));
	  if (Ptr == NULL) {
	    perror("malloc");
	    exit(0);
	  }
	  sscanf(line, "%s %s %s\n",
		 Ptr->hostname, Ptr->ipaddr, Ptr->community);
	  Ptr->Next = Head;
	  Head = Ptr;
	  Num++;
      }
  }
  fclose(config);
  return(Num);
}
  
/**********************************************************************/

int main(int argc, char **argv)
{
  oid *query[10]; 
  int querylen[10];
  int **response;
  
  oid IntakeTemp[14]  = { 1, 3, 6, 1, 4, 1, 9, 9, 13, 1, 3, 1, 3, 1 };
  oid HotPntTemp[14]  = { 1, 3, 6, 1, 4, 1, 9, 9, 13, 1, 3, 1, 3, 2 };
  oid ColdPntTemp[14] = { 1, 3, 6, 1, 4, 1, 9, 9, 13, 1, 3, 1, 3, 3 };

  char *Config = NULL;
 
  /* Hideous argument parsing, 'cause nothing standard knows how to handle
   * long arguments.
   */
  {
    int arg;

    for (arg=1; arg<argc; arg++) {

      /* Check this arg */

      if (argv[arg][0] == '-') {
        if (argv[arg][1] != '-') {
	  
          /* Short args */
          if (argv[arg][1] == 'v')
            Version();

          else if (argv[arg][1] == 'c')
	    Config = argv[++arg];

          else
            Usage(argv[arg]);
        } else {

          /* Long args */
          if (!strcmp(&(argv[arg][2]), "version"))
            Version();

          else if (!strncmp(&(argv[arg][2]), "config=", 7))
	    Config = (argv[arg]+7);

          else
            Usage(argv[arg]);

        } /* End of long args */
      } else {
        goto EndOfDashArgs;
      } /* End of - args */

    } /* End of loop */

  EndOfDashArgs:
    /* ASSERT:  argv[arg] is the first non-parse argument */
    ;
  }

  init_mini_snmp_client(readconf(Config));

  while(1){
    struct Device *Ptr;

    Ptr = Head;
    while(Ptr) {
      query[0] = IntakeTemp;
      query[1] = HotPntTemp;
      query[2] = ColdPntTemp;

      querylen[0] = querylen[1] = querylen[2] = 14;

      response = (int **)snmp_mini_get(Ptr->ipaddr,
				       Ptr->community,
				       3, query, querylen, NULL);
     
      if (response) {
	printf("%.*s - %3d\260 - %3d\260 - %3d\260\n",
	       14, Ptr->hostname,
	     (((*(response[0]))*9)/5)+32,
	     (((*(response[1]))*9)/5)+32,
	     (((*(response[2]))*9)/5)+32);
      }
      else
	printf("%.*s - SNMP Error\n", 14, Ptr->hostname);
      Ptr=Ptr->Next;
    }

    sleep(60);
  }      
}
    




