#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include "template.h"
#include "netmap.h"
#include "ksnmplib_protos.h"

void ask_seq();
void ask_par();
void Decode_Procs_Seq();
void Decode_Procs_Par();

extern char ownpname[];

/*---------------------------------------------------------------*/
/*        Definitions for the User Variable Dictionary           */
/*---------------------------------------------------------------*/

struct var_dict_type var_dict_usr[20];

int var_number_usr = 0;


/*---------------------------------------------------------------*/
/*        Definitions for the User Command Dictionary            */
/*---------------------------------------------------------------*/
struct cmd_dict_type cmd_dict_usr[20];

int cmd_number_usr = 0;

int getint(char *procname, char *varname)
{
  struct var_dict_type var_struct;

  if(Get_Value(procname,varname,&var_struct) != SUCCESS)
    {
      printf("Error in getint(%s,%s)\n",procname,varname);
      return 0;
    }

  if(var_struct.type != INTEGER) {
      printf("Error in getint(%s,%s); not an integer\n",procname,varname);
      return 0;
  }

  return(*(int *)var_struct.addr);
}

char *getstring(char *procname, char *varname)
{
  struct var_dict_type var_struct;

  if(Get_Value(procname,varname,&var_struct) != SUCCESS)
    {
      printf("Error in getstring(%s,%s)\n",procname,varname);
      return((char *) NULL);
    }

  if(var_struct.type != STRING) {
      printf("Error in getstring(%s,%s); not a string\n",procname,varname);
      return((char *) NULL);
  }

  return((char *)var_struct.addr);
}

int main(int argc, char *argv[])
{
  int  loop_flag,i,j;
  int  create_flag=0;
  char destination[80];
  
  strcpy(ownpname, "ysd_status");
  
  if (Attach_Table() == RETURN_ERROR){  
    printf("Error in Attach_Table()\n");
    exit(0);
  }                /*  Map the common table */
  
  init_mib();  /* initialize the MIB variables */
  ksnmp_init();  /* initialize ksnmp   */

  
  if (argc == 1)
    strcpy(destination, "ybosspydaemon");
  else
    strcpy(destination, argv[1]);

  printf("\n==================================================================\n");
  printf("rsd_status of process %s\n",destination);
  printf("\n==================================================================\n");

  {
    int msecs,bytes,runstate;

    runstate = getint(destination,"runstate");
    printf("State: ");
    switch (runstate)
      {
      case UNDEFINED_STATE:
	printf("Undefined");
	break;
      case INIT_STATE:
	printf("Initialized");
	break;
      case RUNNING_STATE:
	printf("Running");
	break;
      case PAUSE_STATE:
	printf("Paused");
	break;
      }
    printf("\n\n");

    if (getint(destination,"at_EOF")==0)
      {
	printf("Reading from file %s\n",
	       getstring(destination,"fname"));
      }
    else
      {
	printf("Not reading.\n");
      }
    if ((runstate==RUNNING_STATE) || (runstate==PAUSE_STATE))
      printf("Writing to circ. buf. %s of size %i\n",
	     getstring(destination,"lname"),
	     getint(destination,"circsize"));

    printf("\n");
    {
      int skip = getint(destination,"skip");

      if (skip>0)
	printf("Skipping %i events out of %i\n",
	       skip,skip+1);
    }
    printf("Packing up to %i events per packet or up to %i bytes per packet\n",
	   getint(destination,"maxevents"),
	   getint(destination,"bufsize"));
    printf("\n");
    printf("%i timouts of up to %.3fms up to now.\n",
	   getint(destination,"timeouts"),
	   1.e-3*getint(destination,"timeout_us"));
    printf("Ignored %i packets.\n",
	   getint(destination,"ignored"));
    
    printf("\n");

    msecs = getint(destination,"sg_msecs");
    if (msecs>0)
      {
	char timestr[256];
	time_t timeval;

	timeval = getint(destination,"sg_uptime");
	strftime(timestr,255,"%H:%M:%S",
		 localtime(&timeval));
	printf("Long term statistics: (uptime: %s)\n",timestr);
	printf("Running %.1fs CPU occupancy %.2f%%\n",
	       msecs*1.e-3,
	       getint(destination,"sg_cpu")*1.e-7);
	bytes = getint(destination,"sg_bytes");
	printf("Received %i packets or %i bytes => %.2f bytes/s\n",
	       getint(destination,"sg_packs"),
	       bytes,bytes*1.e3/msecs);
	printf("\n");
      }

    msecs = getint(destination,"sl_msecs");
    if (msecs>0)
      {
	char timestr[256];
	time_t timeval;

	timeval = getint(destination,"sl_uptime");

	strftime(timestr,255,"%H:%M:%S",
		 localtime(&timeval));
	printf("Short term statistics: (uptime: %s)\n",timestr);
	printf("Period %.1fs CPU occupancy %.2f%%\n",
	       msecs*1.e-3,
	       getint(destination,"sl_cpu")*1.e-7);
	bytes = getint(destination,"sl_bytes");
	printf("Received %i packets or %i bytes => %.2f bytes/s\n",
	       getint(destination,"sl_packs"),
	       bytes,bytes*1.e3/msecs);
	printf("\n");
      }
  }
  printf("==================================================================\n");

  if (Detach_Table() == RETURN_ERROR){
    if (debug_level >= 10) printf("Error in Detach_Table()\n");
  }
  if (create_flag) Delete_Table();
}