From 7f2759484a6bd99ccbdd50ec460a42e0b47bf756 Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Fri, 14 Jun 2013 15:54:34 -0400 Subject: [PATCH] StatsD Monitoring It's a bit hard to have the jailed process communicate with the parent. This is a workaround and is really gross. Please forgive me. --- monitor/.ruby-gemset | 1 + monitor/.ruby-version | 1 + monitor/Gemfile | 3 +++ monitor/Gemfile.lock | 10 ++++++++++ monitor/monitor.rb | 35 +++++++++++++++++++++++++++++++++++ tmate-slave.c | 4 ++-- 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 monitor/.ruby-gemset create mode 100644 monitor/.ruby-version create mode 100644 monitor/Gemfile create mode 100644 monitor/Gemfile.lock create mode 100755 monitor/monitor.rb diff --git a/monitor/.ruby-gemset b/monitor/.ruby-gemset new file mode 100644 index 00000000..81e38e59 --- /dev/null +++ b/monitor/.ruby-gemset @@ -0,0 +1 @@ +tmate diff --git a/monitor/.ruby-version b/monitor/.ruby-version new file mode 100644 index 00000000..518940c5 --- /dev/null +++ b/monitor/.ruby-version @@ -0,0 +1 @@ +ruby-1.9.3-p327 diff --git a/monitor/Gemfile b/monitor/Gemfile new file mode 100644 index 00000000..beba1c97 --- /dev/null +++ b/monitor/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org/' + +gem 'statsd-instrument' diff --git a/monitor/Gemfile.lock b/monitor/Gemfile.lock new file mode 100644 index 00000000..86b1119b --- /dev/null +++ b/monitor/Gemfile.lock @@ -0,0 +1,10 @@ +GEM + remote: https://rubygems.org/ + specs: + statsd-instrument (1.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + statsd-instrument diff --git a/monitor/monitor.rb b/monitor/monitor.rb new file mode 100755 index 00000000..60fc780b --- /dev/null +++ b/monitor/monitor.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby + +# It's a bit hard to have the jailed process communicate with the parent. +# This is a workaround and is really gross. Please forgive me. + +require 'rubygems' +require 'bundler' +Bundler.require + +require 'logger' + +StatsD.server = 'monitor:8125' +StatsD.logger = Logger.new(STDERR) +StatsD.mode = 'production' + +loop do + server_count = 0 + client_count = 0 + + Dir['/proc/*/cmdline'].map do |f| + if File.open(f).read =~ /^tmate-slave \[(.+)\] \((.+)\) (.+)$/ + token = $1 + role = $2 + ip = $3 + + server_count += 1 if role == 'server' + client_count += 1 if role == 'client' + end + end + + StatsD.gauge('tmate.server_count', server_count) + StatsD.gauge('tmate.client_count', client_count) + + sleep 10 +end diff --git a/tmate-slave.c b/tmate-slave.c index 30cc278f..55b4d99e 100644 --- a/tmate-slave.c +++ b/tmate-slave.c @@ -83,8 +83,8 @@ static void set_session_token(struct tmate_ssh_client *client, memset(cmdline, 0, cmdline_end - cmdline); sprintf(cmdline, "tmate-slave [%s] %s %s", tmate_session_token, - client->ip_address, - client->role == TMATE_ROLE_SERVER ? "(server)" : ""); + client->role == TMATE_ROLE_SERVER ? "(server)" : "(client)", + client->ip_address); } static char tmate_token_digits[] = "abcdefghijklmnopqrstuvwxyz"