-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.ph
179 lines (141 loc) · 3.31 KB
/
nginx.ph
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# copyright, 2015 Andres Vargas <[email protected]>
use strict;
use WWW::Curl::Easy;
# Allow reference to collectl variables, but be CAREFUL as these should be treated as readonly
our ($miniFiller, $rate, $SEP, $datetime, $intSecs, $showColFlag);
# Global to this module
my (@now, @last, $schema, $host, $port, $uri);
sub nginxInit
{
my $impOptsref = shift;
my $impKeyref = shift;
my ($flag, $value);
$schema = "http";
$host = "localhost";
$port = "80";
$uri = "nginx_status";
my @opts=split(/,/,$$impOptsref);
foreach my $opt ( @opts) {
$opt =~ /([shpu])=(.*)/;
$flag = $1;
$value = $2;
error("invalid value for option $flag import nginx") if $value eq "";
$schema = $value if $flag eq "s";
$host = $value if $flag eq "h";
$port = $value if $flag eq "p";
$uri = $value if $flag eq "u";
}
$$impOptsref='s';
$$impKeyref='nx';
@now = (0, 0, 0);
@last = (0, 0, 0);
return(1);
}
sub nginxUpdateHeader {}
sub nginxGetData
{
my $response_body;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_URL, "$schema://$host:$port/$uri");
$curl->setopt(CURLOPT_TIMEOUT, 1);
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
my $retcode = $curl->perform;
my ($active, $handled, $accepts);
if ($retcode == 0) {
my @lines = split(/\n/, $response_body);
$lines[0] =~ /Active connections: (\d+)/;
$active = $1 || 0;
$lines[2] =~ / (\d+) (\d+) (\d+)/;
$accepts = $1 || 0;
$handled = $2 || 0;
record(2, "nx-0 $active");
record(2, "nx-1 $accepts");
record(2, "nx-2 $handled");
}
}
sub nginxInitInterval {}
sub nginxAnalyze
{
my $type = shift;
my $dataref = shift;
$type =~/^nx-(.*)/;
my $index = $1;
if ($index) {
if ($$dataref > $last[$index]) {
$now[$index] = $$dataref - $last[$index];
} else {
$now[$index] = $$dataref;
}
$last[$index] = $$dataref;
} else {
$now[$index] = $$dataref;
}
}
sub nginxPrintBrief
{
my $type=shift;
my $lineref=shift;
if ($type==1) # header line 1
{
$$lineref.="<-------Nginx------->";
}
if ($type==2) # header line 1
{
$$lineref.=" Act Acc Han ";
}
elsif ($type==3) # data
{
$$lineref.=sprintf(" %5d %5d %5d ", $now[0], $now[1], $now[2]);
}
}
sub nginxPrintVerbose
{
my $printHeader=shift;
my $homeFlag= shift;
my $lineref= shift;
my $line=$$lineref='';
if ($printHeader)
{
$line.="\n" if !$homeFlag;
$line.="# NGINX STATISTICS ($rate)\n";
$line.="#$miniFiller Act Acc Han \n";
}
$$lineref.=$line;
return if $showColFlag;
$$lineref.=sprintf("$datetime %5d %5d %5d \n", $now[0], $now[1], $now[2]);
$$lineref.=$line;
}
sub nginxPrintPlot
{
my $type= shift;
my $ref1= shift;
if ($type==1)
{
$$ref1.="[NX] Act Acc Han ${SEP}";
}
if ($type==3)
{
$$ref1.=sprintf("$SEP %5d %5d %5d ",
$now[0], $now[1], $now[2]);
}
}
sub nginxPrintExport
{
my $type=shift;
my $ref1=shift;
my $ref2=shift;
my $ref3=shift;
if ($type eq 'g')
{
push @$ref1, "ngix.conn.active";
push @$ref2, 'num';
push @$ref3, $now[0];
push @$ref1, "ngix.conn.accepted";
push @$ref2, 'num';
push @$ref3, $now[1];
push @$ref1, "ngix.conn.handled";
push @$ref2, 'num';
push @$ref3, $now[2];
}
}
1;