Thread fork stdout (9 answers)
Opened by Gast at 2009-01-15 21:14

Gast Gast
 2009-01-17 16:07
#118126 #118126
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
use warnings;
use strict;
use File::Spec::Functions;


my $dir = '/home/mm/.t_m';
my $name = 'naviga3';
my $max_MB = 95;

mkdir( $dir ) or die "$!" if ! -d $dir;
my $file = catfile( $dir, $name );


sub count_traffic {
        my $dev = '/proc/net/dev';
        my $gesamt = 0;
        my $traffic = 0;
        my $summe = 0;
        my $mb_traffic=0;
        my @date = localtime();
        my $day = $date[3];
        my $month = $date[4] + 1;
        my $year = $date[5] + 1900;
        my $pidofpppd;
        
        if ( ! -f $file ) {
                open( my $fh, '>', $file ) or die "$!";
                        print $fh "$day:$month:$year:$traffic\n";
                close( $fh );
        }
        
        open( my $in, '<', $file ) or die "$!";
        while ( <$in> ) {
                $gesamt = $1 if /^$day:$month:$year:(\d+)$/;
        }
        close( $in );
        
        chomp( $pidofpppd = `pidof pppd` );
        kill 15, $pidofpppd if $pidofpppd;
        system( "wvdial &" );

        sub catch_signal { my $pidofwvdial = `pidof wvdial`; kill( 15, $pidofwvdial ); };
        local $SIG{INT} = \&catch_signal;
        local $SIG{QUIT} = \&catch_signal;
        local $SIG{KILL} = \&catch_signal;
        local $SIG{TERM} = \&catch_signal;

        while ( 1 ) {
                chomp( $pidofpppd = `pidof pppd` );
                last if $pidofpppd;
                select( undef, undef, undef, 0.25 );
        }

        while ( 1 ) {
                open( my $proc_net, '<', $dev ) or die "$!";
                while ( <$proc_net> ) {
                        if ( $_ =~ /^\s*ppp0:\s+(\d+)(?:\s+\d+){7}\s+(\d+)(?:\s+\d+){7}\s*$/ )
                                {
                                my $summe = $1 + $2;
                                $traffic = $gesamt + $summe;
                                $mb_traffic = $traffic / 1048576;
                                printf "%.2f\n", $mb_traffic;
                                my @d = localtime();
                                if ( $d[3] != $day ) {
                                        $summe=0;
                                        $gesamt=0;
                                        $traffic=0;
                                }
                                if ( $mb_traffic > $max_MB ) {
                                        $pidofpppd = `pidof pppd`;
                                        kill 15, $pidofpppd;
                                        #write_to_file( $traffic );
                                        exit;
                                }
                        }
                }
                close( $proc_net );
                select( undef, undef, undef, 0.25 );
                chomp( my $pidofpppd = `pidof pppd` );
                last if ! $pidofpppd;
        }
        END { write_to_file( $traffic ); };
}

count_traffic();

View full thread fork stdout