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; }
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 ); } } } }
main( int argc, char **argv ) { thread_t tidHeartbeat; int nbytes_orb; int nbytes_tp; MSG_LOGO putlogo; /* logo of message */ int res; TracePacket tp; int pktid; char srcname[STRSZ]; double mytime; char *rawpkt = NULL; struct Packet *Pkt = NULL; struct PktChannel *pktchan = NULL; int ichan; int bufsize = 0; char *sitedb; int rc; if ( argc != 2 ) { fprintf( stderr, "Usage: orb2eworm <configfile>\n" ); exit( 0 ); } chdir_ewparams( "orb2eworm" ); elog_init( argc, argv ); orb2eworm_config( argv[1] ); logit_init( "orb2eworm", (short) MyModId, MAX_LOGMSG_SIZE, LogSwitch ); logit( "", "%s: Read command file <%s>\n", argv[0], argv[1] ); orb2eworm_lookup( ); if( ( sitedb = getenv( "SITE_DB" ) ) == NULL ) { fprintf(stderr, "Environment variable SITE_DB not defined; "); fprintf(stderr, "orb2eworm exiting.\n"); exit( -1 ); } read_site_db( sitedb ); OrbFd = orbopen( OrbName, "r&" ); if( OrbFd < 0 ) { logit( "e", "Failed to connect to orb %s--Bye\n", OrbName ); exit( -1 ); } else { logit( "", "Connected to orb %s\n", OrbName ); } if( ! STREQ( ChannelSelect, "" ) ) { orbselect( OrbFd, ChannelSelect ); } if( ! STREQ( ChannelReject, "" ) ) { orbreject( OrbFd, ChannelReject ); } tport_attach( &Region, RingKey ); logit( "", "orb2eworm: Attached to public memory region %s: %d\n", RingName, RingKey ); StartThread( Heartbeat, 0, &tidHeartbeat ); putlogo.instid = InstId; putlogo.mod = MyModId; putlogo.type = TypeTracebuf; for( ;; ) { terminate_if_requested(); /* Accept that heartbeats will be delayed for now while orbreap blocks */ rc = orbreap( OrbFd, &pktid, srcname, &mytime, &rawpkt, (int *)&nbytes_orb, &bufsize ); if( rc < 0 ) { elog_clear_register( 1 ); continue; } rc = unstuffPkt( srcname, mytime, rawpkt, nbytes_orb, &Pkt ); if( rc == 0 ) { logit( "et", "Unstuff failure in orb2eworm for %s\n", srcname ); elog_clear_register( 1 ); continue; } for( ichan = 0; ichan < Pkt->nchannels; ichan++ ) { pktchan = gettbl( Pkt->channels, ichan ); if( pktchan_to_tracebuf( pktchan, &tp, mytime, &nbytes_tp ) ) continue; res = tport_putmsg( &Region, &putlogo, nbytes_tp, tp.msg ); } } }
int BBA_DasPar( ClientData clientData , char *srcname, double pkttime, char *packet, int nbytes ) { char cmdstr1[256], cmdstr2[256], cmdstr3[256], key[128]; PktChannel *achan; DPars *dp; int i, alert, val; float fval = 0.0; TclOrb *tclorb = (TclOrb *) clientData; switch (unstuffPkt (srcname, pkttime, packet, nbytes, &unstuff)) { case Pkt_wf: for (i = 0; i < unstuff->nchannels; i++) { achan = (PktChannel *) gettbl (unstuff->channels, i); sprintf (key, "%s_%s\0", achan->sta, achan->chan ); alert = 0; dp = ( DPars *) getarr (tclorb->dpars, key); if ( dp != 0) { val = achan->data[0]; dp->crnt_val = val; if( strncmp(achan->chan, "LLOCK", 5) == 0 ) { alert = check_llock( dp, val ); } else if( strncmp(achan->chan, "BATT", 4) == 0 ) { fval = (float) val / 100.0; dp->crnt_val = fval; alert = check_batt( dp, fval ); } else if( strncmp(achan->chan, "TEMP", 4) == 0 ) { fval = (float) val / 100.0; dp->crnt_val = fval; } if( strlen(dp->widget) > 0 && tclorb->parcallback != 0 ) { dp->min_val = ( dp->crnt_val < dp->min_val )?dp->crnt_val:dp->min_val; dp->max_val = ( dp->crnt_val > dp->max_val )?dp->crnt_val:dp->max_val; dp->ptime = pkttime; if( alert ) { dp->alert = 1; sprintf ( &cmdstr2[0], "%s {%s} {%.2f} {red} \0", tclorb->parcallback, dp->widget, dp->crnt_val ); if( alert < 0 ) sprintf ( &cmdstr1[0], "%s {%s} {%.2f} {red} \0", tclorb->parcallback, dp->minwidget, dp->min_val ); else sprintf ( &cmdstr3[0], "%s {%s} {%.2f} {red} \0", tclorb->parcallback, dp->maxwidget, dp->max_val ); } else { sprintf ( &cmdstr1[0], "%s {%s} {%.2f} {none}\0", tclorb->parcallback, dp->minwidget, dp->min_val ); sprintf ( &cmdstr2[0], "%s {%s} {%.2f} {none}\0", tclorb->parcallback, dp->widget, dp->crnt_val ); sprintf ( &cmdstr3[0], "%s {%s} {%.2f} {none}\0", tclorb->parcallback, dp->maxwidget, dp->max_val ); if( dp->alert ) { sprintf ( &cmdstr2[0], "%s {%s} {%.2f} {LemonChiffon}\0", tclorb->parcallback, dp->widget, dp->crnt_val ); } dp->alert = 0; } if (tclorb->verbose > 0) { printf ("%s\n", cmdstr1 ); printf ("%s\n", cmdstr2 ); printf ("%s\n", cmdstr3 ); fflush (stdout); } Tcl_Eval (tclorb->interp, cmdstr1 ); Tcl_Eval (tclorb->interp, cmdstr2 ); Tcl_Eval (tclorb->interp, cmdstr3 ); } } } break; default: break; } return 1; }