0
0

More than 5 years have passed since last update.

Nginxのリクエスト数/秒をcloudwatchへ連携する

Last updated at Posted at 2018-10-22

nginx stubをonにしておく。

curl http://hoge/server-stub

Active connections: 17 
server accepts handled requests
 1177899 1177899 7047859 
Reading: 0 Writing: 1 Waiting: 16 

差分を取得するrubyメソッド

nginxのstubだと、起動してからの累計しか取れないため、
定期的に差分を取得=>差分秒で割る

req_per_sec.rb
# get request count per sec from nginx stub
def req_per_sec stub_url, conf_path
    now = Time.now

    last_json = FileTest.exists?(conf_path) && JSON.parse(File.read(conf_path))

    txt = `curl #{stub_url}`
    req_counts = txt.split("\n")[2].split(" ")[0].to_i
    File.write conf_path, JSON::generate({time:now, req_counts:req_counts})

    if last_json
        diff_sec = now - Time.parse(last_json['time'])
        req_per_sec = (req_counts - last_json['req_counts']) / diff_sec
        return req_per_sec
    else
        return nil
    end
end

call the method

p req_per_sec('http://hoge/server-stub','/path/to/hoge.json')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0