#/* clpsend.awk - based on telmax/telcap clu poll calls sources #** sendcal.c capn3.c #*/ #/* #************************************************************************* #** * #** Copyright (C) 1997 Trans-Mit Pty. Ltd. Melbourne Australia * #** This software may not be loaned, resold, changed or copied * #** in any way without the express written permission of author * #** and Trans-Mit Pty. Ltd., Australia. * #** * #************************************************************************* #** #** $Header: /u/catcom/AccTraf/src/pcclu/RCS/clpsend.awk,v 1.5 2004/06/16 03:56:42 catcom Exp $ #** $Author: catcom $ #** $Date: 2004/06/16 03:56:42 $ #** $Locker: $ #** #************************************************************************* #*/ function getdtnum(dt) #/* convert datetime string to number dt=ddmmyyHHMMSS */ { year = substr(dt,5,2) + 0; if ( year < 84 ) year += 2000; else year += 1900; return ( (substr(dt,7,6) + 0) + \ ((substr(dt,1,2) + 0) * 1000000) + \ ((substr(dt,3,2) + 0) * 100000000) + \ (year * 10000000000) ); } function getoldmonth() #/* get oldest call data month directory */ { tmpfile = sprintf("/tmp/clptmp%d", rand() * 10000); if ( debug ) printf ("tmpfile = [%s]\n",tmpfile); cmd=sprintf("cd %s;ls -d ??_???? 1>%s 2>/dev/null", cdrdir, tmpfile); system(cmd); oldyyyymm=999999 while ((getline monthdir < tmpfile) > 0) { mm = substr(monthdir,1,2) + 0; yyyy = substr(monthdir,4,4) + 0; yyyymm = (yyyy * 100) + mm; if ( yyyymm < oldyyyymm ) oldyyyymm = yyyymm; } close(tmpfile); cmd=sprintf("rm %s 1>/dev/null 2>&1", tmpfile); system(cmd); if ( debug ) printf("clpsend: getoldmonth() oldyyyymm = [%s]\n", oldyyyymm); } function cleancdr() { for ( ;; ) { if ( firsttime ) { mfirst = 1; # flag first for month getoldmonth(); # find oldest call data month directory */ if ( debug ) printf("clpsend: cleancdr() first sdatetime = [%s]\n", sdatetime); firsttime = 0; /* start at oldest data */ yyyy = int(oldyyyymm / 100); mm = oldyyyymm - (yyyy * 100); dd = 1; hh = 0; if ( debug ) printf("clpsend: cleancdr() start at oldest %s.\n",oldyyyymm); } else { #/* inc day */ if ( ++dd > 31 ) { dd = 1; mfirst = 1; if ( ++mm > 12 ) { mm = 1; ++yyyy; } } if ( debug ) printf("clpsend: cleancdr() day incremented to %02d%02d%04d\n", dd, mm, yyyy); } if ( mfirst ) { /* first for month */ #/* if month dir not found then assume no more data */ monthdir = sprintf("%s/%02d_%04d", cdrdir, mm, yyyy); if ( debug ) printf("clpsend: cleancdr() monthdir = [%s]\n", monthdir); cmd = sprintf("ls %s >/dev/null 2>&1", monthdir); status = system(cmd); if ( status != 0 ) { #/* no more files */ printf("clpsend: cleancdr() month dir [%s] NOT FOUND\n", monthdir); return -1; } if ( debug ) printf("clpsend: cleancdr() month dir FOUND\n"); mfirst = 0; } #/* skip day if day dir doesn't exist */ daydir = sprintf("%s/%02d_%02d_%04d", monthdir, dd, mm, yyyy); cmd = sprintf("ls %s >/dev/null 2>&1", daydir); status = system(cmd); if ( status != 0 ) { #/* no more files */ if ( debug ) printf("clpsend: cleancdr() day dir NOT FOUND\n"); hh = 23; #/* force next day */ continue; } if ( debug ) printf("clpsend: cleancdr() day dir FOUND\n"); #/* month & day dir exists break; } "uname -n" | getline pcclunodename rawarcfile = sprintf("%04d%02d%02d_%s.raw", yyyy, mm, dd, pcclunodename); cmd = "rm -f /u/catcom/clp/raw/" rawarcfile if ( debug ) printf("clpsend: cleancdr() clean raw cmd = [%s]\n", cmd ); status = system(cmd); if ( status != 0 ) { # delete raw failed printf("clpsend: cleancdr() system(%s) ERROR = %d\n", cmd, daydir, status); } if ( debug ) printf("clpsend: cleancdr() daydir = [%s]\n", daydir ); if ( debug ) printf("rm -f %s/*", daydir); cmd = sprintf("rm -f %s/*", daydir); status = system(cmd); if ( status != 0 ) { # delete failed printf("clpsend: cleancdr() system(%s) ERROR = %d\n", cmd, daydir, status); return -1; } # remove any empty dirs if ( debug ) printf("(rmdir %s; rmdir %s) 1>/dev/null 2>&1", daydir, monthdir); cmd = sprintf("(rmdir %s; rmdir %s) 1>/dev/null 2>&1", daydir, monthdir); status = system(cmd); return 0; } function setcdrfile() { for ( ;; ) { if ( firsttime ) { mfirst = 1; # flag first for month getoldmonth(); # find oldest call data month directory */ if ( debug ) printf("clpsend: setcdrfile() first sdatetime = [%s]\n", sdatetime); firsttime = 0; #/* set from global if first time */ dd = substr(sdatetime,1,2) + 0; mm = substr(sdatetime,3,2) + 0; year = substr(sdatetime,5,2) + 0; if ( year < 84 ) year += 2000; else year += 1900; yyyy = year; hh = substr(sdatetime,7,2); syyyymm = (yyyy * 100) + mm; if ( oldyyyymm > syyyymm ) { /* start at oldest data */ yyyy = int(oldyyyymm / 100); mm = oldyyyymm - (yyyy * 100); dd = 1; hh = 0; if ( debug ) printf("clpsend: setcdrfile() start at oldest.\n"); } } else { #/* inc hour */ if ( ++hh > 23 ) { hh = 0; if ( ++dd > 31 ) { dd = 1; mfirst = 1; if ( ++mm > 12 ) { mm = 1; ++yyyy; } } } if ( debug ) printf("clpsend: setcdrfile() hour incremented to %02d%02d%04d:%02d\n", dd, mm, yyyy, hh); } if ( mfirst ) { /* first for month */ #/* if month dir not found then assume no more data */ monthdir = sprintf("%s/%02d_%04d", cdrdir, mm, yyyy); if ( debug ) printf("clpsend: setcdrfile() monthdir = [%s]\n", monthdir); cmd = sprintf("ls %s >/dev/null 2>&1", monthdir); status = system(cmd); if ( status != 0 ) { #/* no more files */ if ( debug ) printf("clpsend: setcdrfile() month dir NOT FOUND\n"); return 0; } if ( debug ) printf("clpsend: setcdrfile() month dir FOUND\n"); mfirst = 0; } #/* skip day if day dir doesn't exist */ daydir = sprintf("%s/%02d_%02d_%04d", monthdir, dd, mm, yyyy); cmd = sprintf("ls %s >/dev/null 2>&1", daydir); status = system(cmd); if ( status != 0 ) { #/* no more files */ if ( debug ) printf("clpsend: setcdrfile() day dir NOT FOUND\n"); hh = 23; #/* force next day */ continue; } if ( debug ) printf("clpsend: setcdrfile() day dir FOUND\n"); #/* month & day dir exists break; } #/* get cdr file and path to it */ cdrfile = sprintf("%s/%02d_%02d_%04d:%02d", daydir, dd, mm, yyyy, hh); if ( debug ) printf("clpsend: setcdrfile() cdrfile = [%s]\n", cdrfile ); return 1; } function callget(strttime,endtime) #/* find next call in requested period */ { for ( ;; ) { if ( cdrfile == "" ) { if ( !setcdrfile() ) return 0; } #/* loop for each call */ contbadreccnt = 0; while ( (getline cdrrec < cdrfile) > 0 ) { clufields = split(cdrrec,cdrarr,"|"); #printf("clpsend.awk: [%s] clufields=%d\n", substr(cdrarr[1],1,1), clufields ) >> logfile; # validate start char & correct number of fields # and call datetime if ( substr(cdrarr[1],1,1) != ":" || clufields != 8 || length(cdrarr[4]) != 12 || match(cdrarr[4],"[^0-9]") ) { if ( ++contbadreccnt > MAXCONTBADRECS ) { printf("clpsend.awk: Aborting after %d bad recs\n", contbadreccnt) >> logfile; thiscount = -1; return 0; } printf("clpsend.awk: Skipping bad rec %d\n", contbadreccnt) >> logfile; continue; } if ( contbadreccnt > 0 ) printf("clpsend.awk: Continuing after %d bad recs\n", contbadreccnt) >> logfile; contbadreccnt = 0; ###################### # assume a good record # strip ":" cdrarr[1] = substr(cdrarr[1],2); # get time field calltime = getdtnum(cdrarr[4]); if ( debug ) { printf("cdrrec = [%s] cdrarr[1] = [%s] cdrarr[4] = [%s]\n", cdrrec, cdrarr[1], cdrarr[4]); printf("clpsend: calltime = [%f] [%s]\n", calltime, cdrarr[4]); printf("clpsend: strttime = [%f]\n", strttime); printf("clpsend: endtime = [%f]\n", endtime); } #/* call ends in required period */ if ( calltime >= strttime && calltime < endtime ) { ++thiscount; if ( debug ) printf("GOTONE %d\n", thiscount); return 1; } #/* call ends after the period */ if ( calltime >= endtime ) { if ( debug ) printf("PASTEND %d\n", endcnt); if ( ++endcnt >= 20 ) #/* stop 20 beyond end*/ return 0; #/* finished search */ } else { if ( debug ) printf("BEFORESTART\n"); } } close(cdrfile); cdrfile = ""; } } BEGIN { srand(); if ( debug ) printf("clpsend: Top of BEGIN\n"); #/* init global vars */ FS = "|"; logfile = "/u/catcom/clp/log" MAXCONTBADRECS = 10; DEFCDRDIR = "."; cdrfile = ""; if ( cdrdir == "" ) cdrdir = DEFCDRDIR; firsttime = 1; cdrrec = ""; thiscount = -1; endcnt = 0; if ( clean == 1 ) { exit ( cleancdr() ); } stime = getdtnum(sdatetime); etime = getdtnum(edatetime); startcount = blkcount; if ( debug ) { printf("clpsend: cdrdir = [%s]\n", cdrdir); printf("clpsend: sdatetime = [%s] edatetime = [%s] blkcount = [%d]\n", sdatetime, edatetime, blkcount); printf("clpsend: stime = [%f] etime= [%f]\n", stime, etime); } printf("STARTCLPSEND:\n"); while ( callget(stime,etime) ) { if ( thiscount >= startcount ) { printf("%ld%s\n", thiscount, cdrrec); } } printf("ENDCLPSEND:\n"); if ( debug ) { printf("\nPoll calls: END: %ld calls less skip of %ld = %ld.\n", thiscount, startcount, thiscount - startcount ); } exit ( errcode ); } { if ( debug ) printf("clpsend: Top of Main Loop\n"); } END { if ( debug ) printf("clpsend: Top of END\n"); }