ovcullfile
上传用户:shbosideng
上传日期:2013-05-04
资源大小:1555k
文件大小:3k
源码类别:

SNMP编程

开发平台:

C/C++

  1. #!/usr/local/bin/perl5
  2. # -*- mode: Perl -*-
  3. ##################################################################
  4. # Remove old data from OV_DB files
  5. ##################################################################
  6. # Created by Laurie Gellatly <gellatly@one.net.au>
  7. # This reads an OV_DB file for the cut off and interface
  8. # and calculates how many lines can be removed
  9. #################################################################
  10. #
  11. # Distributed under the GNU copyleft
  12. #
  13. # $Id: ovcullfile,v 1.1.1.1 2002/02/26 10:16:36 oetiker Exp $
  14. #
  15. use strict;
  16. use vars '$DEBUG';
  17. my $ov_db = "/var/opt/OV/share/databases";
  18. my $ovdb= $ov_db."/snmpCollect/";
  19. my $DEBUG = 0;
  20.    my($days2keep, $interface, $io , $cutoff) ;
  21.    my($cnt, $data, $recno, $junk);
  22.    my @names = ( "IfInErrors", "IfOutErrors", 
  23.      "IfInOctets", "IfOutOctets", "avgBusy5");
  24.    my @namext = (".err", "", ".cpu");
  25.    my ($val);
  26.    my $recsz = 24;
  27.    my $fsize = -1;
  28.    my $p = "',$p'";
  29.    my $parms = '-F\t '{printf("%d\t%d\t%s\t%lg\n", $4, $5, $6, $3)}'';
  30.    my @resu;
  31. sub main {
  32.    my($extn) = "";
  33.    $days2keep = $ARGV[0];
  34.    $interface = $ARGV[1];
  35.    $io = $ARGV[2];
  36.    die <<USAGE  unless (defined($days2keep) && defined($interface) && defined($io));
  37. USAGE: ovcullfile '#days Interface# io&errs=1|cpu=0'
  38. EXAMPLE:  ovcullfile 30 4 1
  39. Delete all data older than 30 days for 
  40. all interface 4 Input and Output Octets & Errors
  41. USAGE
  42.    $cutoff = time - ( 3600 * 24 * $days2keep) ;
  43.    $extn =  $namext[$io];
  44.    if ($io) {
  45.       get(2);
  46.       get(3);
  47.       get(0);
  48.       get(1);
  49.    } else {
  50.       get(4);
  51.    }
  52. }
  53.   
  54. main;
  55. exit(0);
  56. sub get {
  57.    my ($ind) = @_;
  58.    my ($starttime, $entime, $IPAdd1, $IPAdd2, $IPAdd3, $IPAdd4, $cnt, $val) ;
  59.    my ($file) = $ovdb.$names[$ind].".".$interface;
  60.    open (RAWIN , $file) or die "Could not open $file";
  61.    my ($junk, $junk, $junk, $junk, $junk, $junk, $junk, $cfsize) = stat $file;
  62.    binmode(RAWIN);
  63.    my ($notfound) = (($cfsize / $recsz) - 1)> -1;
  64.    if (($fsize == $cfsize) && ($recno > 1)){
  65.       seek(RAWIN,($recno - 2) * $recsz, 0);
  66.       $notfound = ($recsz == read(RAWIN, $data, $recsz));
  67.       ($starttime, $entime, $IPAdd1, $IPAdd2, $IPAdd3, $IPAdd4, $cnt, $val) =
  68.       unpack("LLCCCCLd",$data);
  69.       if ($cutoff < $entime) {
  70.          seek(RAWIN, 0, 0);
  71.  $recno = 1;
  72.       }
  73.    } else {
  74.       $fsize = $cfsize;
  75.       $recno = 1;
  76.    } 
  77.    while ($notfound){
  78. # Read from the beginning of file
  79.       $notfound = ($recsz == read(RAWIN, $data, $recsz));
  80.       ($starttime, $entime, $IPAdd1, $IPAdd2, $IPAdd3, $IPAdd4, $cnt, $val) =
  81.       unpack("LLCCCCLd",$data);
  82.       if ($cutoff < $entime) {
  83.  $notfound = 0;
  84.       } else {
  85.  $recno++;
  86.       }
  87.    }
  88.    close (RAWIN);
  89.    if ($recno > 1){
  90.    @resu = `snmpColDump -tTI $file | sed -n $recno$p |
  91.        awk $parms |
  92.        snmpColDump -r - $file`
  93. #       wc -l`
  94.    }
  95.    return ($recno);
  96. }