-
Notifications
You must be signed in to change notification settings - Fork 2
/
nginx_status
executable file
·169 lines (156 loc) · 6.15 KB
/
nginx_status
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
#!/usr/bin/env ruby1.8
require 'open-uri'
require 'ostruct'
#$VERBOSE = nil
class MuninNginxStatus
class Graph < OpenStruct
end
GRAPH_DATA = {
:default => Graph.new(
:config_graph => [
["graph_title" , "Nginx Status" ] ,
["graph_category" , "nginx" ] ,
["graph_args" , "--base 1000 -l 0" ] ,
["graph_vlabel" , "Connections, Requests" ] #
] ,
:item_list => [
:active_connections ,
:waiting ,
:accepted_connections ,
:handled_requests #
] #
) ,
:request => Graph.new(
:config_graph => [
["graph_title" , "Nginx Number of Requests"] ,
["graph_category" , "nginx" ] ,
["graph_args" , "--base 1000 -l 0" ] ,
["graph_vlabel" , "Requests / sec" ] #
] ,
:item_list => [
:handled_requests #
] #
) ,
:connection => Graph.new(
:config_graph => [
["graph_title" , "Nginx Number of Connections"] ,
["graph_category" , "nginx" ] ,
["graph_args" , "--base 1000 -l 0" ] ,
["graph_vlabel" , "Connections" ] #
] ,
:item_list => [
:active_connections ,
:reading ,
:writing ,
:waiting #
] #
) ,
:all => Graph.new(
:config_graph => [
["graph_title" , "Nginx Status" ] ,
["graph_category" , "nginx" ] ,
["graph_args" , "--base 1000 -l 0" ] ,
["graph_vlabel" , "Connections, Requests" ] #
] ,
:item_list => [
:active_connections ,
:reading ,
:writing ,
:waiting ,
:accepted_connections ,
:handled_connections ,
:handled_requests #
] #
) #
}
DEFAULT_GRAPH_TYPE = :default
DEFAULT_NGINX_STATUS_URL = "http://localhost/nginx_status"
module NginxStatus
class Item < OpenStruct
end
ITEM_LIST_ALL = [
Item.new( :name => :active_connections , :re_index => 1 , :label => "Active connections" , :ctype => "GAUGE" , :info => "Number of all open connections." ) ,
Item.new( :name => :accepted_connections , :re_index => 2 , :label => "Accepted connections/sec" , :ctype => "DERIVE" , :min => 0 , :info => "Number of accepted connections." ) ,
Item.new( :name => :handled_connections , :re_index => 3 , :label => "Handled connections/sec" , :ctype => "DERIVE" , :min => 0 , :info => "Number of handled connections (no one was closed just it was accepted)." ) ,
Item.new( :name => :handled_requests , :re_index => 4 , :label => "Requests/sec" , :ctype => "DERIVE" , :min => 0 , :info => "Number of handled requests." ) ,
Item.new( :name => :reading , :re_index => 5 , :label => "Reading connections" , :ctype => "GAUGE" , :info => "nginx reads request header" ) ,
Item.new( :name => :writing , :re_index => 6 , :label => "Writing connections" , :ctype => "GAUGE" , :info => "nginx reads request body, processes request, or writes response to a client" ) ,
Item.new( :name => :waiting , :re_index => 7 , :label => "Keep-Alive connections" , :ctype => "GAUGE" , :info => "keep-alive connections, actually it is active - (reading + writing)") ###
]
PARSE_RE = %r{Active connections: (\d+) .*? (\d+) (\d+) (\d+) .*?Reading: (\d+) Writing: (\d+) Waiting: (\d+)}i
# http://wiki.nginx.org/HttpStubStatusModule
# The stub status module reports status similar to mathopd's status page. It is plain text information like
# | Active connections: 291
# | server accepts handled requests
# | 16630948 16630948 31070465
# | Reading: 6 Writing: 179 Waiting: 106
# active connections -- number of all open connections
# server accepts handled requests -- nginx accepted 16630948 connections, handled 16630948 connections (no one was closed just it was accepted), and handles 31070465 requests (1.8 requests per connection)
# reading -- nginx reads request header
# writing -- nginx reads request body, processes request, or writes response to a client
# waiting -- keep-alive connections, actually it is active - (reading + writing)
def self.parse_page(page)
m = PARSE_RE.match((page||"").gsub(/\s+/," "))
if m
ret = {}
ITEM_LIST_ALL.each { |i|
ret[i.name] = m[i.re_index]
}
ret
else
nil
end
end
end
def initialize(h)
@env = h[:env]
@arg = h[:arg]
@filename = h[:filename]
@graph_data = ([email protected](/_/).last.to_sym; GRAPH_DATA[x] || GRAPH_DATA[DEFAULT_GRAPH_TYPE])
@nginx_status_url = @env["nginx_status_url"] || DEFAULT_NGINX_STATUS_URL
end
def retrieve_page
open(@nginx_status_url){|f| f.read } rescue nil
end
def exec
status = NginxStatus.parse_page(retrieve_page())
case @arg
when "autoconf"
if status
puts "yes"
else
puts "no"
end
when "config"
@graph_data.config_graph.each { |x|
puts x.join(" ")
}
puts_list(:type => :label)
puts_list(:type => :info )
puts_list(:type => :ctype )
puts_list(:type => :min )
else
puts_list(:type => :value, :status => status)
end
end
def puts_list(h)
@graph_data.item_list.each { |n|
item = NginxStatus::ITEM_LIST_ALL.detect{|i| i.name == n }
case h[:type]
when :label
puts [item.name, ".label", " ", item.label].join
when :info
puts [item.name, ".info" , " ", item.info ].join
when :ctype
puts [item.name, ".type" , " ", item.ctype ].join
when :min
puts [item.name, ".min" , " ", item.min ].join if item.min
when :value
puts [item.name, ".value", " ", h[:status][item.name]].join
end
}
end
end
if __FILE__ == $0
MuninNginxStatus.new(:arg => ARGV[0], :env => ENV, :filename => $0).exec
end