File: slashmon

package info (click to toggle)
slash 2.2.6-8etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,672 kB
  • ctags: 1,915
  • sloc: perl: 23,113; sql: 1,878; sh: 433; makefile: 233
file content (150 lines) | stat: -rwxr-xr-x 3,204 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2001 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.

use File::Basename;
use FindBin '$Bin';
use Getopt::Std;
use Slash::Utility;
use Slash::DB;

my $PROGNAME = basename($0);
(my $PREFIX = $Bin) =~ s|/[^/]+/?$||;

my %opts;
$opts{'u'} ||= $ARGV[0] || 'slashdot';

createEnvironment($opts{'u'});

my $slashdb = getCurrentDB();
my $constants = getCurrentStatic();

my $sleep = $ARGV[1] || 10;
my $rows = $ARGV[2] || 50;


my $log=$slashdb->sqlSelectMany("host_addr,uid,op,dat,
                     date_format(ts,\"\%H:\%i\") as ts,id",
                     "accesslog","id > ?",
                     "ORDER BY ts DESC");

my $proc=$slashdb->sqlShowProcessList();


my ($id)=$slashdb->sqlSelect("max(id)","accesslog"); 
my $starttime=time;
my $startsq=getQuestions();



while(1) {
	$proc->execute();
	print "\n\n";
	# system("clear");
	$active=0;
	undef @p;
	while(my $S=$proc->fetchrow_hashref() ) {
		next if $$S{Info} eq "accesslog";
		next if $$S{Command} eq "Sleep";
		next if $$S{Info} eq "SHOW PROCESSLIST";
		next unless $$S{Info};

		$active++;
		my $o=$$S{Time}." ";
		$o.=" " if $$S{Time} < 10;

		$$S{Info}=~s/\n//g;
             	$$S{Info}=~s/[^_\'\-\+\.A-Z0-9\(\) ,=\|]//gi;
		$$S{Info}=~s/(\s)+/ /g;
		$$S{Info}=~s/, /,/g;
		$$S{Info}=~s/\) /\)/g;
		$$S{Info}=~s/DATE_ADD\((.*)\)//g;
		$$S{Info}=~s/DATE_FORMAT\((.*)\)//g;
		$$S{Info}=~s/SELECT/S/;
		$o.=substr($$S{Info},0,76);
		push @p,$o;

	}
	$proc->finish();

	open P, "/proc/loadavg";
	@la=split " ",<P>;
	$l="$la[0] $la[1] $la[2]";
	close P;

	my $oldsec=$sec;

	$oldsq=$sq;
	$oldsq||=1;
	$sq=getQuestions();
	$thissq=$sq - $oldsq;
	$totalsq=$sq - $startsq;

    	$sec=time - $starttime;
	my $timepassed=$sec - $oldsec;
	$sec||=1;
	$timepassed||=1;

	$log->bind_param(1,$id);
	$log->execute;

	my $oldtotal=$total;
    	$total+=$log->rows;
	my $thistotal=$total - $oldtotal;
	$total||=1;
	$thistotal||=1;

	print "Total:$sec ".
		"pages:$total ".
		"avg:".sprintf("%.2f",$total/$sec)." ".
		"sql:$totalsq ".
		"avg:".sprintf("%.2f",$totalsq/$sec)." ".
		"pp:".sprintf("%.2f",$totalsq/$total )."\n".

	       "  Now:$timepassed ".
		"pages:$thistotal ".
		"avg:".	sprintf("%.2f",$thistotal/$timepassed)." ".
		"sql:$thissq ".
		"avg:". sprintf("%.2f",$thissq/$timepassed)." ".
		"pp:".sprintf("%.2f",$thissq/$thistotal)."\n";
		
        print "  Sql:$active/".$proc->rows()." | $l\n";
        	

    	while(my $U=$log->fetchrow_hashref()) {
	        my $w="$$U{ts} $$U{host_addr} ($$U{uid})";
	        $w.= "\t" if length $w < 24;
	        $w.= "\t$$U{op}";
       	 	$w.= "\t" if length($$U{op}) < 8;
	        $w.= "\t$$U{dat}\n";
	        # print $w unless $ARGV[0] eq "sec";
	        $id=$$U{id} if $$U{id} > $id;
    	}
    	$proc->finish();

                  
	my $x=0;
 	foreach (sort{ $b <=> $a }@p ) {
		$x++;
		next if $x > $rows;
		print "$_\n";
        }
	print "\n";

	sleep $sleep;
}



sub getQuestions
{
	my $c=$slashdb->sqlShowStatus();
	$c->execute();
	while (my ($q,$a)=$c->fetchrow() ) {
		$r=$a if $q eq "Questions";
	}
	$c->finish();
	return $r;

}