int main (int argc, char **argv) { int c, errflg = 0; char *in, *out; int orbin, orbout; double maxpkts = VERY_LARGE_NUMBER ; int quit; char *pktmatch = strdup(".*/pf/evtinfo"), *reject = 0; int nmatch; int specified_after = 0; double after = 0.0, until = VERY_LARGE_NUMBER ; double start_time, end_time, delta_t ; double totpkts = 0, totbytes = 0; Flags flags; static int last_pktid = -1; static double last_pkttime = 0.0; char *statefile = 0; double last_burial = 0.0; double decent_interval = 300.0; int mode = PKT_NOSAMPLES; int rcode; char srcname[ORBSRCNAME_SIZE]; double pkttime = 0.0 ; int pktid; int nbytes; char *packet = 0; int packetsz = 0; Packet *unstuffed = 0; Pf *pf; Tbl *tbl; Arr *arr; char *arrkey; char *pffilename; Tbl *channels; char *net; char *sta; char *chan; char netstachan[ORBSRCNAME_SIZE]; Srcname parts; double maxtime,mintime,starttime,triggertime,duration,min_g,max_g,snr; char *datalogger; int evtfilesize,channelno,maxcts,mincts; char *errors,*evtfilename,*ft,*filter,*srcid; char *line; int i; Pf *pfnew; Dbptr db; int put_tests=0; char *chanmatch=0; Hook *hook=0; char *tdummy=0; char *match=malloc(100); memset (&flags, 0, sizeof (flags)); elog_init (argc, argv); elog_notify (0, "%s $Revision: 1.5 $ $Date: 2005/05/10 07:34:43 $\n", Program_Name); while ((c = getopt (argc, argv, "m:n:r:S:c:tvV")) != -1) { switch (c) { case 'm': match = optarg; sprintf(pktmatch,"%s/pf/evtinfo",match); break; case 'c': chanmatch = optarg; break; case 't': put_tests = 1; break; case 'n': maxpkts = atoi (optarg); break; case 'r': reject = optarg; break; case 'S': statefile = optarg; break; case 'v': flags.verbose++; break; case 'V': flags.verbose++; flags.verbose++; break; case '?': errflg++; } } if (errflg || argc - optind < 2 || argc - optind > 4) usage (); in = argv[optind++]; out = argv[optind++]; if (argc > optind) { after = str2epoch (argv[optind++]); specified_after = 1; if (argc > optind) { until = str2epoch (argv[optind++]); if (until < after) { until += after ; } } } if ((orbin = orbopen (in, "r&")) < 0) die (0, "Can't open input '%s'\n", in); if (statefile != 0) { char *s; if (exhume (statefile, &quit, RT_MAX_DIE_SECS, 0) != 0) { elog_notify (0, "read old state file\n"); } if (orbresurrect (orbin, &last_pktid, &last_pkttime) == 0) { elog_notify (0, "resurrection successful: repositioned to pktid #%d @ %s\n", last_pktid, s = strtime (last_pkttime)); free (s); } else { complain (0, "resurrection unsuccessful\n"); } } if ((orbout = orbopen (out, "w&")) < 0) { die (0, "Can't open output '%s'\n", out); } if (pktmatch) { nmatch = orbselect (orbin, pktmatch); } if (nmatch < 0) { die (1, "select '%s' returned %d\n", pktmatch, nmatch); } if (reject) { nmatch = orbreject (orbin, reject); } if (nmatch < 0) { elog_die (1, "reject '%s' returned %d\n", reject, nmatch); } else { if (flags.verbose) { elog_notify (1,"%d sources selected\n", nmatch); } } if (specified_after) { pktid = orbafter (orbin, after); if (pktid < 0) { char *s; elog_complain (1, "seek to %s failed\n", s = strtime (after)); free (s); pktid = forbtell (orbin); elog_complain (1,"pktid is still #%d\n", pktid); } else { if (flags.verbose) elog_notify (1,"new starting pktid is #%d\n", pktid); } } start_time = now (); db = dbtmp("rt1.0"); while (!quit && pkttime < until && totpkts < maxpkts) { rcode = orbreap (orbin, &pktid, srcname, &pkttime, &packet, &nbytes, &packetsz); switch (rcode) { case 0: totpkts++; totbytes += nbytes; if (flags.verbose>2) { showPkt (pktid, srcname, pkttime, packet, nbytes, stdout, mode); } if (statefile != 0 && last_pkttime - last_burial > decent_interval) { bury (); last_burial = pkttime; } if ((unstuffPkt (srcname, pkttime, packet, nbytes, &unstuffed))==Pkt_pf) { pf = unstuffed->pf; tbl= pfkeys(pf); arrkey= gettbl(tbl,0); pfget(pf,arrkey,&pfnew); datalogger= pfget_string(pfnew,"datalogger"); duration= pfget_double(pfnew,"duration"); errors= pfget_string(pfnew,"errors"); evtfilename= pfget_string(pfnew,"evtfilename"); evtfilesize= pfget_int(pfnew,"evtfilesize"); ft= pfget_string(pfnew,"ft"); starttime= pfget_double(pfnew,"time"); triggertime= pfget_double(pfnew,"triggertime"); channels=pfget_tbl(pfnew,"channels"); if (strcasecmp(ft,"no")==0 || put_tests == 1) { for (i=0; i<maxtbl(channels);i++) { line=gettbl(channels,i); sscanf(line,"%s %d %lf %d %lf %lf %d %lf", netstachan,&channelno,&maxtime,&maxcts,&max_g,&mintime,&mincts,&min_g); split_srcname(netstachan,&parts); strcpy(parts.src_suffix,"GENC"); join_srcname(&parts,netstachan); if (flags.verbose>1) printf("%s,%s\n", netstachan,tdummy=strtime(triggertime)); if (chanmatch == 0 || strmatches(parts.src_chan,chanmatch,&hook)) { if (flags.verbose) elog_notify(1,"putting detev for %s,%s\n", netstachan,tdummy=strtime(triggertime)); db=dblookup(db,0,"detev",0,"dbSCRATCH"); dbputv(db,0,"sta",parts.src_sta,"chan",parts.src_chan, "filter",K2_FILTER, "time", triggertime, "tron", starttime - triggertime, "troff", starttime - triggertime + duration, "iphase","K2", "snr",(max_g * 1000.0 - min_g * 1000.0)/2.0, 0); db2orbpkt(db,orbout); } else { if (flags.verbose>1) elog_notify(1,"ignoring %s,%s\n", netstachan,tdummy=strtime(triggertime)); } } } } last_pktid = pktid; last_pkttime = pkttime; } } if (statefile != 0) bury (); end_time = now (); delta_t = end_time - start_time; if (flags.verbose>1) { if (totpkts > 0) { elog_notify (1,"\n%.0f %.2f byte packets (%.1f kbytes) in %.3f seconds\n\t%10.3f kbytes/s\n\t%10.3f kbaud\n\t%10.3f pkts/s\n", totpkts, totbytes / totpkts, totbytes / 1024, delta_t, totbytes / delta_t / 1024, totbytes / delta_t / 1024 * 8, totpkts / delta_t); } else { elog_notify (1,"\nno packets read\n"); } } if (orbclose (orbin)) { elog_complain (1, "error closing read orb\n"); } if (orbclose (orbout)) { elog_complain (1, "error closing write orb\n"); } return 0; }
static void id_clients( Pf *pf ) { Pf *pfclients; Pf *pfclient; char client_summary[STRSZ]; char *serveraddress; char *serverport; Tbl *client_keys; int ikey; char *client_key; char clientid_key[STRSZ]; char *clientaddress; char *what; char clientid[33]; pfeval( pf, "server{address}", &serveraddress ); pfeval( pf, "server{port}", &serverport ); pfget( pf, "clients", (void **) &pfclients ); client_keys = pfkeys( pfclients ); for( ikey = 0; ikey < maxtbl( client_keys ); ikey++ ) { client_key = gettbl( client_keys, ikey ); pfget( pfclients, client_key, (void **) &pfclient ); clientaddress = pfget_string( pfclient, "address" ); what = pfget_string( pfclient, "what" ); sprintf( client_summary, "%s %s %s %s", serveraddress, serverport, clientaddress, what ); sprintf( clientid_key, "clients{%s}{clientid}", client_key ); mdhex( clientid, client_summary, strlen( client_summary ) ); pfset( pf, clientid_key, clientid ); } freetbl( client_keys, 0 ); return; }
static void pfmorph( Pf *pf ) { Pf *dlspf; Pf *stapf; Tbl *stas; int ista; char *sta; char *opt_string; if( pfget( pf, "dls", (void **) &dlspf ) == PFINVALID ) { return; } stas = pfkeys( dlspf ); for( ista = 0; ista < maxtbl( stas ); ista++ ) { sta = gettbl( stas, ista ); if( pfget( dlspf, sta, (void **) &stapf ) == PFINVALID ) { continue; } if( ( opt_string = pfget_string( stapf, "opt" ) ) == (char *) NULL || ! strcmp( opt_string, "-" ) ) { pfset( stapf, "acok", "-" ); pfset( stapf, "api", "-" ); pfset( stapf, "isp1", "-" ); pfset( stapf, "isp2", "-" ); pfset( stapf, "ti", "-" ); } else { if( strcontains( opt_string, "acok", NULL, NULL, NULL ) ) { pfset( stapf, "acok", "1" ); } else { pfset( stapf, "acok", "0" ); } if( strcontains( opt_string, "api", NULL, NULL, NULL ) ) { pfset( stapf, "api", "1" ); } else { pfset( stapf, "api", "0" ); } if( strcontains( opt_string, "isp1", NULL, NULL, NULL ) ) { pfset( stapf, "isp1", "1" ); } else { pfset( stapf, "isp1", "0" ); } if( strcontains( opt_string, "isp2", NULL, NULL, NULL ) ) { pfset( stapf, "isp2", "1" ); } else { pfset( stapf, "isp2", "0" ); } if( strcontains( opt_string, "ti", NULL, NULL, NULL ) ) { pfset( stapf, "ti", "1" ); } else { pfset( stapf, "ti", "0" ); } } } freetbl( stas, 0 ); return; }
int main( int argc, char **argv ) { int c; int errflag = 0; int orb; int stop = 0; long nrecs; char *match = ".*/pf/st"; char *from = 0; char *statefile = 0; char *pfname = "orb2rrdc"; char *orbname; char *dbcache; char *rrdtool; char command[STRSZ]; char net[STRSZ]; char sta[STRSZ]; char rrdvar[STRSZ]; char key[STRSZ]; char path[FILENAME_MAX]; Dbptr db; Dbptr dbt; Pf *pf; char *Default_network; Tbl *dlslines; Arr *Dls_vars_dsparams; Arr *Dls_vars_rras; Tbl *Dls_vars_keys; char *line; char *dls_var; char *dsparams; Tbl *rras; int i; int j; OrbreapThr *ort; int pktid; char srcname[ORBSRCNAME_SIZE]; double time = 0; char *packet = 0; int nbytes = 0; int bufsize = 0; Packet *pkt = 0; int rc; char *s; Pf *dlspf; Tbl *dlspfkeys; char *element; Tbl *parts; double val; Pf *pfval = 0; elog_init( argc, argv ); while( ( c = getopt( argc, argv, "vVd:s:p:m:f:" ) ) != -1 ) { switch( c ) { case 'd': CacheDaemon = optarg; break; case 'f': from = optarg; break; case 'm': match = optarg; break; case 'p': pfname = optarg; break; case 's': statefile = optarg; break; case 'v': Verbose++; break; case 'V': VeryVerbose++; Verbose++; break; default: elog_complain( 0, "Unknown option '%c'\n", c ); errflag++; break; } } if( errflag || argc - optind != 2 ) { usage(); } if( Verbose ) { elog_notify( 0, "Starting at %s (%s $Revision$ $Date$)\n", zepoch2str( str2epoch( "now" ), "%D %T %Z", "" ), Program_Name ); } orbname = argv[optind++]; dbcache = argv[optind++]; pfread( pfname, &pf ); rrdtool = pfget_string( pf, "rrdtool" ); if( rrdtool == NULL || ! strcmp( rrdtool, "" ) ) { elog_die( 0, "Error: no rrdtool executable name specified in parameter file\n" ); } else if( ( rrdtool[0] == '/' && ! is_present( rrdtool ) ) || ( rrdtool[0] != '/' && ! datafile( "PATH", rrdtool ) ) ) { elog_die( 0, "Error: can't find rrdtool executable by name of '%s' (check PATH environment " "variable, or absolute path name if given)\n", rrdtool ); } else if( rrdtool[0] == '/' ) { sprintf( command, "%s -", rrdtool ); } else { sprintf( command, "rrdtool -" ); } Suppress_egrep = pfget_string( pf, "suppress_egrep" ); if( Suppress_egrep != NULL && strcmp( Suppress_egrep, "" ) ) { if( ! datafile( "PATH", "egrep" ) ) { elog_complain( 0, "Ignoring suppress_egrep parameter: can't find egrep on path\n" ); } else { sprintf( command, "%s 2>&1 | egrep -v '%s'", command, Suppress_egrep ); } } if( VeryVerbose ) { elog_notify( 0, "Executing command: %s\n", command ); } Rrdfp = popen( command, "w" ); if( Rrdfp == (FILE *) NULL ) { elog_die( 0, "Failed to open socket to rrdtool command\n" ); } orb = orbopen( orbname, "r&" ); if( orb < 0 ) { elog_die( 0, "Failed to open orb '%s' for reading. Bye.\n", orbname ); } orbselect( orb, match ); if( from != NULL && statefile == NULL ) { pktid = orbposition( orb, from ); if( Verbose ) { elog_notify( 0, "Positioned to packet %d\n", pktid ); } } else if( from != NULL ) { elog_complain( 0, "Ignoring -f in favor of existing state file\n" ); } if( statefile != NULL ) { stop = 0; exhume( statefile, &stop, 15, 0 ); orbresurrect( orb, &pktid, &time ); if( Verbose ) { elog_notify( 0, "Resurrecting state to pktid %d, time %s\n", pktid, s = strtime( time ) ); free( s ); } orbseek( orb, pktid ); } dbopen( dbcache, "r+", &db ); if( db.database < 0 ) { elog_die( 0, "Failed to open cache database '%s'. Bye.\n", dbcache ); } else { db = dblookup( db, "", "rrdcache", "", "" ); if( db.table < 0 ) { elog_die( 0, "Failed to lookup 'rrdcache' table in '%s'. Bye.\n", dbcache ); } } dbcrunch( db ); dbt = dbsubset( db, "endtime == NULL", NULL ); Rrd_files = newarr( 0 ); dbquery( dbt, dbRECORD_COUNT, &nrecs ); for( dbt.record = 0; dbt.record < nrecs; dbt.record++ ) { dbgetv( dbt, 0, "net", &net, "sta", &sta, "rrdvar", &rrdvar, NULL ); dbfilename( dbt, (char *) &path ); sprintf( key, "%s:%s:%s", net, sta, rrdvar ); if( ! is_present( path ) ) { elog_complain( 0, "WARNING: rrd file '%s', listed in database, does not exist. " "Removing database entry.\n", path ); dbmark( dbt ); } else { setarr( Rrd_files, key, strdup( path ) ); if( VeryVerbose ) { elog_notify( 0, "Re-using rrd file '%s' for '%s'\n", path, key ); } } } Rrdfile_pattern = pfget_string( pf, "rrdfile_pattern" ); Status_stepsize_sec = pfget_double( pf, "status_stepsize_sec" ); Default_network = pfget_string( pf, "default_network" ); dlslines = pfget_tbl( pf, "dls_vars" ); Dls_vars_dsparams = newarr( 0 ); Dls_vars_rras = newarr( 0 ); for( i = 0; i < maxtbl( dlslines ); i++ ) { line = gettbl( dlslines, i ); strtr( line, "\t", " " ); rras = split( line, ' ' ); dls_var = shifttbl( rras ); dsparams = shifttbl( rras ); setarr( Dls_vars_dsparams, dls_var, dsparams ); setarr( Dls_vars_rras, dls_var, rras ); } ort = orbreapthr_new( orb, -1., 0 ); for( ; stop == 0; ) { orbreapthr_get( ort, &pktid, srcname, &time, &packet, &nbytes, &bufsize ); if( statefile ) { rc = bury(); if( rc < 0 ) { elog_complain( 0, "Unexpected failure of bury command! " "(are there two orb2rrdc's running with the same state" "file?)\n" ); clear_register( 1 ); } } rc = unstuffPkt( srcname, time, packet, nbytes, &pkt ); if( rc == Pkt_pf ) { if( VeryVerbose ) { /* Parameter files generally too big for elog */ fprintf( stderr, "Received a parameter-file '%s' at %s\n%s\n\n", srcname, s = strtime( time ), pf2string( pkt->pf ) ); free( s ); } else if( Verbose ) { elog_notify( 0, "Received a parameter-file '%s' at %s\n", srcname, s = strtime( time ) ); free( s ); } pfmorph( pkt->pf ); if( VeryVerbose ) { fprintf( stderr, "Morphed parameter-file '%s' to interpret 'opt':\n%s\n\n", srcname, pf2string( pkt->pf ) ); } pfget( pkt->pf, "dls", (void **) &dlspf ); dlspfkeys = pfkeys( dlspf ); Dls_vars_keys = keysarr( Dls_vars_dsparams ); for( i = 0; i < maxtbl( dlspfkeys ); i++ ) { element = gettbl( dlspfkeys, i ); if( strcontains( element, "_", 0, 0, 0 ) ) { parts = split( (s = strdup( element )), '_' ); sprintf( net, "%s", (char *) gettbl( parts, 0 ) ); sprintf( sta, "%s", (char *) gettbl( parts, 1 ) ); free( s ); freetbl( parts, 0 ); } else { sprintf( net, "%s", Default_network ); sprintf( sta, "%s", element ); } for( j = 0; j < maxtbl( Dls_vars_keys ); j++ ) { dls_var = gettbl( Dls_vars_keys, j ); sprintf( key, "%s{%s}", element, dls_var ); if( pfresolve( dlspf, key, 0, &pfval ) < 0 ) { elog_complain( 0, "Unable to extract variable '%s' " "(not present or wrong type) from element '%s' " "in packet from '%s', timestamped '%s'; Skipping\n", key, element, srcname, s = strtime( time ) ); free( s ); pfval = 0; continue; } else if( pfval != (Pf *) NULL && pfval->value.s != (char *) NULL && ! strcmp( pfval->value.s, "-" ) ) { if( VeryVerbose ) { elog_notify( 0, "Non-floating point value '-' in variable '%s', " "in packet from '%s', timestamped '%s'; Skipping data point\n", key, srcname, s = strtime( time ) ); free( s ); } continue; } else { val = pfget_double( dlspf, key ); } archive_dlsvar( db, net, sta, dls_var, (char *) getarr( Dls_vars_dsparams, dls_var ), (Tbl *) getarr( Dls_vars_rras, dls_var ), time, val ); } } freetbl( dlspfkeys, 0 ); freetbl( Dls_vars_keys, 0 ); } else if( rc == Pkt_stash ) { ; /* Do nothing */ } else { if( Verbose ) { elog_notify( 0, "Received a packet that's not a parameter file " "(type '%d' from unstuffPkt); skipping\n", rc ); } } } }
static Pf * orbconnections2pf( Pf *pfanalyze ) { Pf *pf; Pf *pfserver; Pf *pfconnections; Pf *pfclients; Pf *pfclient; Pf *pfconnection; Tbl *client_keys; int ikey; char *client_key; char *clientid; char *what; char *perm; char *clientaddress; char *serveraddress; char clientaddress_ipc[STRSZ]; char serveraddress_ipc[STRSZ]; int serverport; double atime; regex_t preg_findclient; char closeorb[STRSZ]; char o2omachine[STRSZ]; char farorb[STRSZ]; int closeport; int farport; char orbstat_machine_hostname[STRSZ]; char orbstat_machine_ipc[STRSZ]; int orbstat_machine_ip; char cmdline_fromorb[STRSZ]; char cmdline_toorb[STRSZ]; char cmdline_fromip[STRSZ]; char cmdline_fromipc[STRSZ]; char cmdline_toip[STRSZ]; char cmdline_toipc[STRSZ]; char formal_name[STRSZ]; int formal_count = 0; int cmdline_fromport; int cmdline_toport; struct in_addr addr; regcomp( &preg_findclient, "^orb2orb ", 0 ); pf = pfnew( PFFILE ); atime = pfget_time( pfanalyze, "client_when" ); pfput_time( pf, "connections_when", atime ); my_ip( orbstat_machine_hostname, orbstat_machine_ipc, &orbstat_machine_ip ); if( is_localhost( orbstat_machine_ipc ) ) { elog_complain( 0, "libpforbstat: orbstat machine is localhost; giving up on connection analysis\n" ); regfree( &preg_findclient ); return pf; } else { pfput_string( pf, "orbstat_machine", orbstat_machine_ipc ); } pfget( pfanalyze, "server", (void **) &pfserver ); serveraddress = pfget_string( pfserver, "address" ); serverport = pfget_int( pfserver, "port" ); if( is_nonroutable( serveraddress ) && report_nonroutable( serveraddress ) ) { elog_complain( 0, "libpforbstat: warning: monitored server %s is nonroutable\n", serveraddress ); } if( name2ip( serveraddress, &addr, serveraddress_ipc ) < 0 ) { elog_complain( 0, "libpforbstat: warning: name translation failed for %s\n", serveraddress ); strcpy( serveraddress_ipc, serveraddress ); } if( is_localhost( serveraddress ) ) { strcpy( closeorb, orbstat_machine_ipc ); } else { strcpy( closeorb, serveraddress_ipc ); } closeport = serverport; /* SCAFFOLD: this causes memory problems. Leave untranslated for now: pfput_string( pfserver, "address", closeorb ); Record the translated server address */ pfget( pfanalyze, "clients", (void **) &pfclients ); client_keys = pfkeys( pfclients ); pfconnections = pfnew( PFTBL ); for( ikey = 0; ikey < maxtbl( client_keys ); ikey++ ) { client_key = gettbl( client_keys, ikey ); pfget( pfclients, client_key, (void **) &pfclient ); what = pfget_string( pfclient, "what" ); if( ! regexec( &preg_findclient, what, 0, 0, 0 ) ) { pfconnection = pfnew( PFARR ); /* Easy things: */ pfput_string( pfconnection, "what", what ); if( ( clientid = pfget_string( pfclient, "clientid") ) != NULL ) { pfput_string( pfconnection, "clientid", clientid ); } /* Preparatory raw-information acquisition: */ extract_orb2orb_orbargs( what, cmdline_fromorb, cmdline_toorb ); parse_orbname( cmdline_fromorb, cmdline_fromip, &cmdline_fromport ); if( name2ip( cmdline_fromip, &addr, cmdline_fromipc ) < 0 ) { elog_complain( 0, "libpforbstat: warning: name translation failed for %s\n", cmdline_fromipc ); strcpy( cmdline_fromipc, cmdline_fromip ); } parse_orbname( cmdline_toorb, cmdline_toip, &cmdline_toport ); if( name2ip( cmdline_toip, &addr, cmdline_toipc ) < 0 ) { elog_complain( 0, "libpforbstat: warning: name translation failed for %s\n", cmdline_toipc ); strcpy( cmdline_toipc, cmdline_toip ); } perm = pfget_string( pfclient, "perm" ); clientaddress = pfget_string( pfclient, "address" ); if( name2ip( clientaddress, &addr, clientaddress_ipc ) < 0 ) { elog_complain( 0, "libpforbstat: warning: name translation failed for %s\n", clientaddress ); strcpy( clientaddress_ipc, clientaddress ); } /* Analysis */ if( is_nonroutable( clientaddress ) && report_nonroutable( clientaddress ) ) { elog_complain( 0, "libpforbstat: warning: clientaddress %s is nonroutable\n", clientaddress ); } if( is_localhost( clientaddress ) ) { strcpy( o2omachine, serveraddress_ipc ); } else { strcpy( o2omachine, clientaddress_ipc ); } pfput_string( pfconnection, "o2omachine", o2omachine ); if( STREQ( perm, "w" ) ) { strcpy( farorb, cmdline_fromipc ); farport = cmdline_fromport; } else if( STREQ( perm, "r" ) ) { strcpy( farorb, cmdline_toipc ); farport = cmdline_toport; } else { elog_complain( 0, "libpforbstat: unexpected perm '%s' in client info; giving up on client\n", perm ); pffree( pfconnection ); continue; } if(is_localhost(farorb)) { strcpy( farorb, o2omachine ); } if( STREQ( perm, "w" ) ) { pfput_string( pfconnection, "fromaddress", farorb ); pfput_int( pfconnection, "fromport", farport ); pfput_string( pfconnection, "toaddress", closeorb ); pfput_int( pfconnection, "toport", closeport ); pfput_string( pfconnection, "closeorb", "toaddress" ); } else { /* perm == "r" */ pfput_string( pfconnection, "fromaddress", closeorb ); pfput_int( pfconnection, "fromport", closeport ); pfput_string( pfconnection, "toaddress", farorb ); pfput_int( pfconnection, "toport", farport ); pfput_string( pfconnection, "closeorb", "fromaddress" ); } sprintf( formal_name, "client%03d", ++formal_count ); pfput( pfconnections, formal_name, pfconnection, PFPF ); } } pfput( pf, "connections", pfconnections, PFPF ); regfree( &preg_findclient ); return pf; }
static Pf * orbdatabases2pf( Pf *pfanalyze ) { Pf *pf; Pf *pfdatabases; Pf *pfdatabase; Pf *pfclients; Pf *pfclient; double atime; Tbl *client_keys; int ikey; char *client_key; char *what; char *serverhost; char *clientid; char *clientaddress; char *serveraddress; char *serverport; char dbprogram[STRSZ]; char dbpath[STRSZ]; char dir[STRSZ]; char dfile[STRSZ]; char formal_name[STRSZ]; int formal_count = 0; char *delim = ":"; char *hostdir; char *serverhostcopy; char *abspath; pf = pfnew( PFFILE ); pfdatabases = pfnew( PFTBL ); atime = pfget_time( pfanalyze, "client_when" ); pfput_time( pf, "databases_when", atime ); pfeval( pfanalyze, "server{address}", &serveraddress ); pfeval( pfanalyze, "server{port}", &serverport ); pfeval( pfanalyze, "server{host}", &serverhost ); serverhostcopy = strdup( serverhost ); strtok_r( serverhostcopy, delim, &hostdir ); pfget( pfanalyze, "clients", (void **) &pfclients ); client_keys = pfkeys( pfclients ); for( ikey = 0; ikey < maxtbl( client_keys ); ikey++ ) { client_key = gettbl( client_keys, ikey ); pfget( pfclients, client_key, (void **) &pfclient ); what = pfget_string( pfclient, "what" ); clientaddress = pfget_string( pfclient, "address" ); clientid = pfget_string( pfclient, "clientid" ); if( is_dbprogram( what, dbprogram, dbpath ) ) { pfdatabase = pfnew( PFARR ); pfput_string( pfdatabase, "clientid", clientid ); pfput_string( pfdatabase, "serveraddress", serveraddress ); pfput_string( pfdatabase, "serverport", serverport ); pfput_string( pfdatabase, "dbprogram", dbprogram ); if( is_localhost( clientaddress ) ) { pfput_string( pfdatabase, "dbmachine", serveraddress ); abspath = concatpaths( hostdir, dbpath, 0 ); parsepath( abspath, dir, dfile, 0 ); free( abspath ); } else { pfput_string( pfdatabase, "dbmachine", clientaddress ); abspath = concatpaths( "", dbpath, 0 ); parsepath( abspath, dir, dfile, 0 ); free( abspath ); } pfput_string( pfdatabase, "dir", dir ); pfput_string( pfdatabase, "dfile", dfile ); sprintf( formal_name, "client%03d", ++formal_count ); pfput( pfdatabases, formal_name, pfdatabase, PFPF ); } } free( serverhostcopy ); pfput( pf, "databases", pfdatabases, PFPF ); return pf; }
int delegate_to_perl( Dbptr db, Pf *delegate_pf ) { char scriptfile[FILENAME_MAX]; char fifofile[FILENAME_MAX]; char pffile[FILENAME_MAX]; char *commands; char *mypath; char *cwd; FILE *fp; Tbl *keys; char *key; Pf *pfpart; pid_t pid; int status; int i; if( ( mypath = datafile( "PATH", "perl" ) ) == 0 ) { complain( 0, "Couldn't find perl on path.\n" ); return -1; } else if( access( mypath, R_OK|X_OK ) ) { complain( 0, "Couldn't find perl on path.\n" ); return -1; } if( ( commands = pfget_string( delegate_pf, "commands" ) ) == NULL ) { complain( 0, "No commands &Literal in parameter file\n" ); return -1; } cwd = getcwd( NULL, FILENAME_MAX ); sprintf( scriptfile, "/tmp/dbgme_script_%d_%d.pl", getuid(), getpid() ); sprintf( fifofile, "/tmp/dbgme_fifo_%d_%d.fifoview", getuid(), getpid() ); sprintf( pffile, "/tmp/dbgme_pf_%d_%d.pf", getuid(), getpid() ); mkfifo( fifofile, 0775 ); fp = fopen( pffile, "w" ); keys = pfkeys( delegate_pf ); for( i = 0; i < maxtbl( keys ); i++ ) { key = gettbl( keys, i ); if( ! strcmp( key, "commands" ) ) { continue; } pfresolve( delegate_pf, key, 0, &pfpart ); fprintf( fp, "%s ", key ); pfout( fp, pfpart ); } fclose( fp ); fp = fopen( scriptfile, "w" ); fprintf( fp, "@db = dbopen_table( \"%s\", \"r\" );\n", fifofile ); fprintf( fp, "$pf = \"%s\";\n", pffile ); fprintf( fp, "%s\n", commands ); fprintf( fp, "exit\n" ); fclose( fp ); if( ( pid = fork() ) == 0 ) { if( Verbose ) { fprintf( stderr, "Running Perl delegate\n" ); } else { fflush( stderr ); fclose( stdout ); } execlp( "perl", "perl", "-e", scriptfile, NULL ); } else { fp = fopen( fifofile, "w" ); dbwrite_view( db, fp ); fclose( fp ); wait( &status ); } if( Verbose ) { fprintf( stderr, "Perl exit status is %d\n", status ); } free( cwd ); unlink( scriptfile ); unlink( fifofile ); unlink( pffile ); if( status != 0 ) { return -1; } else { return 0; } }
/* This function is used to check a list of pf names to verify they are in the parameter space. It is useful for any program that uses parameter files that cracks the pf space anywhere except up front. That is, the pf space is a convenient place to store all the run time parameters of any program and is a convenient way to pass a large control structure. However, because flow can be complex it is possible to have parameters that are only accessed deep within a program and in these situations it is desirable to run a check at program initialization to verify the required parameters will be there when required. Normally a string variable will not cause pfget to die, but it will here if it is listed as required Parameters to be checked are grouped by type. Each numerical fields can contain an optional range check. This is not allowed for strings. Code below only checks int, double, boolean, and string variables. Because of the complexity of Tbl and Arrs I thought this not worth messing with. Furthermore, it is not unusual to have an empty Tbl list or Arr that the program should handle correctly. Function will die on the first occurence of a problem parameter. It is void because it only returns if everything checks out. Author: Gary L. Pavlis */ void check_required_pf(Pf *pf) { Tbl *t,*testtbl; Pf *pf_required; char *key; int i,j,nitems; int itest, ilowcheck, ihighcheck; double dtest, dlowcheck, dhighcheck; int bool; char *line; char *ctest; char name[50]; /* This cracks the "required" &Arr dies if it isn't present at all. This assumes you wouldn't call this routine if you weren't serious about checking */ if(pfget(pf,"require",(void **)&pf_required) != PFARR) die(0,"Arr of required parameters (require &Arr) missing from parameter space\nMust be present to execute this program\n"); t=pfkeys(pf_required); for(i=0;i<maxtbl(t);++i) { key = gettbl(t,i); if(!strcmp(key,"int")) { testtbl = pfget_tbl(pf_required,"int"); for(j=0;j<maxtbl(testtbl);++j) { line = gettbl(testtbl,i); nitems = sscanf(line,"%s%d%d",name,&ilowcheck, &ihighcheck); itest = pfget_int(pf,name); if(nitems == 3) { if( (itest < ilowcheck) || (itest > ihighcheck) ) die(0, "Parameter %s has value %d which is outside required range of %d to %d\n", name,itest,ilowcheck,ihighcheck); } } } else if(!strcmp(key,"double")) { testtbl = pfget_tbl(pf_required,"double"); for(j=0;j<maxtbl(testtbl);++j) { line = gettbl(testtbl,i); nitems = sscanf(line,"%s%lg%lg",name,&dlowcheck, &dhighcheck); dtest = pfget_double(pf,name); if(nitems == 3) { if( (dtest < dlowcheck) || (dtest > dhighcheck) ) die(0, "Parameter %s has value %lg which is outside required range of %lg to %lg\n", name,dtest,dlowcheck,dhighcheck); } } } else if(!strcmp(key,"boolean")) { testtbl = pfget_tbl(pf_required,"boolean"); for(j=0;j<maxtbl(testtbl);++j) { line = gettbl(testtbl,i); itest = pfget_boolean(pf,line); } } else if(!strcmp(key,"string")) { testtbl=pfget_tbl(pf_required,"string"); for(j=0;j<maxtbl(testtbl);++j) { line = gettbl(testtbl,i); ctest = pfget_string(pf,line); if(ctest == NULL) die(0,"Missing required string variable = %s\n", line); } } else { elog_notify(0,"Unknown required type name = %s\nRequired parameters under this heading will not be checked\n", key); } } freetbl(t,0); }
int delegate_to_matlab( Dbptr db, Pf *delegate_pf ) { char scriptfile[FILENAME_MAX]; char fifofile[FILENAME_MAX]; char pffile[FILENAME_MAX]; char cmd[STRSZ]; char *commands; char *mypath; char *cwd; FILE *fp; Tbl *keys; char *key; Pf *pfpart; pid_t pid; int status; int i; if( ( mypath = datafile( "PATH", "matlab" ) ) == 0 ) { elog_complain( 0, "Couldn't find matlab on path.\n" ); return -1; } else if( access( mypath, R_OK|X_OK ) ) { elog_complain( 0, "Couldn't find matlab on path.\n" ); return -1; } if( ( commands = pfget_string( delegate_pf, "commands" ) ) == NULL ) { elog_complain( 0, "No commands &Literal in parameter file\n" ); return -1; } cwd = getcwd( NULL, FILENAME_MAX ); sprintf( scriptfile, "/tmp/dbgme_script_%d_%d.m", getuid(), getpid() ); sprintf( fifofile, "/tmp/dbgme_fifo_%d_%d", getuid(), getpid() ); sprintf( pffile, "/tmp/dbgme_pf_%d_%d.pf", getuid(), getpid() ); mkfifo( fifofile, 0775 ); fp = fopen( pffile, "w" ); keys = pfkeys( delegate_pf ); for( i = 0; i < maxtbl( keys ); i++ ) { key = gettbl( keys, i ); if( ! strcmp( key, "commands" ) ) { continue; } pfresolve( delegate_pf, key, 0, &pfpart ); fprintf( fp, "%s ", key ); pfout( fp, pfpart ); } fclose( fp ); fp = fopen( scriptfile, "w" ); fprintf( fp, "addpath('%s')\n", cwd ); fprintf( fp, "db = dbread_view( '%s' );\n", fifofile ); fprintf( fp, "pf = dbpf( '%s' );\n", pffile ); fprintf( fp, "%s\n", commands ); fprintf( fp, "exit\n" ); fclose( fp ); sprintf( cmd, "try, " "run %s, " "catch, " "lasterr, " "elog_clear_register('print'), " "unix(['kill -9 ' num2str(getpid)]), " "end", scriptfile ); if( ( pid = fork() ) == 0 ) { if( Verbose ) { fprintf( stderr, "Running Matlab delegate\n" ); } else { fflush( stderr ); fclose( stdout ); } execlp( "matlab", "matlab", "-nodisplay", "-nojvm", "-r", cmd, NULL ); } else { fp = fopen( fifofile, "w" ); dbwrite_view( db, fp ); fclose( fp ); wait( &status ); } if( Verbose ) { fprintf( stderr, "Matlab exit status is %d\n", status ); } free( cwd ); unlink( scriptfile ); unlink( fifofile ); unlink( pffile ); if( status != 0 ) { return -1; } else { return 0; } }