/* * Notes and Copyrights * * Web100 MIB read tool for scripts and file output * Modified by Yee-Ting Li @ HEP, UCL. * * Version 0.4 - 9th Jan 2002 * Added proper help * Verison 0.3 - 8th Oct 2001 * Adapted for alpha0.3 * Version 0.2 - 6th Sept 2001 * * This code comes with no gurantees nor warrentees whatsoever * Use this at your own risk. I accept no resposibilities for * misleading information arising from the use of this program. * */ /* * Web100 MIB read tool for scripts */ #include #include #include #include "web100.h" #include #include #include // simple definations #define MAXVARIABLES 100 #define MAXSTRINGLENGTH 20 main (int argc, char *argv[]) { struct web100_agent *ag; struct web100_connection *conn; struct web100_snap *ns, *os=0; struct web100_var **wv; unsigned int value, delta; unsigned char addr[4]; long long llvalue, lldelta; unsigned int i, number_of_variables; char variables[MAXVARIABLES][MAXSTRINGLENGTH], temp[MAXSTRINGLENGTH]; char type[MAXVARIABLES][6], type_temp[6]; FILE *file_name; int match; struct web100_ftuple *ft; DIR *dirp; struct dirent *direntp; unsigned int cid, max_cid; unsigned char* next; char buffer[5]; // time structures time_t rawtime; struct tm * timeinfo; // stuff for arguments int file_arg=0, out_arg=0; char* file; unsigned char* cid_arg; int time_step = 1000000; // = 1 sec int stop_arg = 0; unsigned int state_var; /* ************************************************************ */ /* Command line argument parsing */ /* ************************************************************ */ void printhelp() { printf( "Readvars Monitors Web100 variables for a specific ConnectionID and outputs results to STDOUT. Usage: readvars -c ConnectionID{cid,next,new} [OPTIONS] -c is the ConnectionID of the TCP stream to be monitored. This can be specified with an integer value corresponding to the ID 'cid' OR the next option based on the highest number open TCP socket 'next' OR the next newly opened TCP socket 'new'. It is recommented that new be used.\n [OPTIONS] are:\n -s terminates monitoring immediately when the ConnectionID specified connection closes. -t time-step of monitoring in Milliseconds. -v is the path to a plain ASCII file containing a specific list of Web100 Variables to be monitored. Exclusion of this option will result in all Variables being monitored. -h prints this help screen.\n Report bugs to .\n\n"); exit(2); } int okay = 0; for ( i=0; id_name[0] != '.' && direntp->d_name[0] != 'h') { cid = atoi (direntp->d_name); if (cid >= max_cid) max_cid = cid; i++; } } closedir (dirp); /* ------------------------------------------------------------ */ /* check to see if next cid is required if so, set cid to the next value */ /* ------------------------------------------------------------ */ next = cid_arg; match = 0; //printf ("Argument for CID is: %s\n",next); printf ("Max CID open: %i\n", max_cid); // want the next open CID based on the highest value of the current /proc/web100 if ( strcmp( cid_arg, "next") == 0 ) cid = max_cid + 1; // want the next newly open cid else if ( strcmp( cid_arg, "new" ) == 0 ) { int max_value = 0; int check=0; printf("Waiting for new connection.\n"); sleep (0.5); cid = 0; // monitoring for changes in the dir and output new dir as cid while ( match == 0 ) { dirp = opendir ("/proc/web100"); for (;;) { direntp = readdir (dirp); if ( direntp == NULL ) break; if ( direntp->d_name[0] != '.' && direntp->d_name[0] != 'h') { // only check numbers above the highest existant value int name = atoi(direntp->d_name); if ( name > max_value ) { max_value = name ; //printf("max_value=%i\n",max_value); } } } closedir (dirp); // first time if ( check == 0 ) check = max_value; else if ( max_value > check ) { cid = max_value; //printf( "Connection id created: %i\n", match ); match = 1; } if (match == 1) break; } } // take in a specified cid number to monitor else cid = atoi (cid_arg); /* ------------------------------------------------------------ */ /* check if selected cid exists - assumes that the newest connection is the highest cid */ /* ------------------------------------------------------------ */ // printf ("Checking file...\n"); sprintf( buffer, "%i", cid ); // need to convert cid to a string printf ("Monitoring Connection ID: %s...\n",buffer); // recurse through folder to check for directory store values in array while ( match == 0 ) { dirp = opendir ("/proc/web100"); for (;;) { direntp = readdir (dirp); if ( direntp == NULL ) break; if (direntp->d_name[0] != '.' && direntp->d_name[0] != 'h') { //printf ("cid: %i\td_name: %i\n",cid, atoi( direntp->d_name) ); if ( cid == atoi( direntp->d_name) ) { //printf ("Match found!\n"); match = 1; } } if ( match == 1 ) break; } closedir (dirp); usleep (50000); //wait for 50 milliseconds } /* ************************************************************ */ /* Set up memory for monitoring */ /* ************************************************************ */ conn=web100_find_connection(ag, buffer ); for (i=0; i < number_of_variables ; i++) { wv[i]=web100_find_var(conn, variables[i]); } /* ------------------------------------------------------------ */ /* take a snapshot */ /* ------------------------------------------------------------ */ ns=web100_get_snapshot(conn, web100_get_group(wv[0])); os = ns; if (ns == NULL) { web100_perror(conn); exit(2); } /* ************************************************************ */ /* Display Connection Settings */ /* ************************************************************ */ printf ("\n==========Web100 Connection Settings==========\n"); // connections ft = web100_get_ftuple (conn->agent, conn->cid); printf ("Local machine at "); printf ("%d.%d.%d.%d", ((u_int32_t) ft->localadd) & 0xff, ((u_int32_t) ft->localadd >> 8) & 0xff, ((u_int32_t) ft->localadd >> 16) & 0xff, ((u_int32_t) ft->localadd >> 24) & 0xff); printf (" monitoring TCP port %u, CID %i\n", ft->localport, cid); printf ("Remote machine connected at "); printf ("%d.%d.%d.%d", ((u_int32_t) ft->remoteadd) & 0xff, ((u_int32_t) ft->remoteadd >> 8) & 0xff, ((u_int32_t) ft->remoteadd >> 16) & 0xff, ((u_int32_t) ft->remoteadd >> 24) & 0xff); printf (" on TCP port %u\n", ft->remoteport); // time step output printf ("Timestep is %i second(s)\n", time_step); // Print out the connection time time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Connection opened at time: %s\n", asctime (timeinfo) ); /* start looped output */ printf ("\n===========Monitoring Web100 Output===========\n"); /* ************************************************************ */ /* Display Header for variables */ /* ************************************************************ */ for (i = 0; i < number_of_variables; i++) printf ("%s (%s)\t", variables[i], type[i]); printf ("\n"); /* ************************************************************ */ /* Display the value of each variable */ /* ************************************************************ */ while ( 1 ) { /* ************************************************************ */ /* Display each varible separatly */ /* ************************************************************ */ for (i=0; itype) { case TYPE_INTEGER: if (match==0) { value=web100_get_integer(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_integer(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_INTEGER32: if (match==0) { value=web100_get_integer32(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_integer32(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_COUNTER32: if (match==0) { value=web100_get_counter32(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_counter32(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_GAUGE32: if (match==0) { value=web100_get_gauge32(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_gauge32(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_UNSIGNED32: if (match==0) { value=web100_get_unsigned32(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_unsigned32(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_TIME_TICKS: if (match==0) { value=web100_get_time_ticks(wv[i], ns); printf("%u\t", value); } else{ delta=web100_delta_time_ticks(wv[i], ns, os); printf("%u\t", delta); } break; case TYPE_IP_ADDRESS: web100_get_IPv4_address(wv[i], ns, addr); printf("%u.%u.%u.%u\t", addr[0], addr[1], addr[2], addr[3]); break; case TYPE_UNSIGNED16: if (match==0) { value=web100_get_unsigned16(wv[i], ns); printf("%u\t", value); } else { delta=web100_delta_unsigned16(wv[i], ns, os); printf("%u\t", delta); } break; /* XXX not fully tested */ case TYPE_COUNTER64: if (match==0) { llvalue=web100_get_counter64(wv[i], ns); printf("%llu\t", llvalue); } else { lldelta=web100_delta_counter64(wv[i], ns, os); printf("%llu\t", lldelta); } break; default: printf("??%s(%d)\t", wv[i]->name, wv[i]->type); } // switch } printf("\n"); /* end output at current moment */ // quit if connection is closed and state == 1 if( stop_arg==1 && web100_get_integer (wv[state_var], ns) == 1 ) { printf ("\n==========Connection Closed==========\n"); time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Connection closed at: %s\n", asctime (timeinfo) ); web100_perror (conn); exit (2); } usleep( time_step ); os=ns; ns=web100_get_snapshot(conn, web100_get_group(wv[0])); /* if ns is nothing, exit */ if ( ns == NULL ) { printf ("\n==========Connection Closed==========\n"); time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Connection closed at: %s\n", asctime (timeinfo) ); web100_perror (conn); exit (2); } } web100_free_connection(conn); }