File: monitor.rb

package info (click to toggle)
tmate-ssh-server 2.3.0-49-g97d20249-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,520 kB
  • sloc: ansic: 39,208; awk: 339; makefile: 252; sh: 126; ruby: 45; perl: 41
file content (62 lines) | stat: -rwxr-xr-x 1,553 bytes parent folder | download | duplicates (2)
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
#!/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'
require 'logger'
require 'set'

Bundler.require

StatsD.server = 'monitor:8125'
StatsD.mode = 'production'
StatsD.logger = Logger.new(STDERR)

hostname = Socket.gethostname
seen_not_paired_tokens = Set.new
seen_paired_tokens = Set.new

loop do
  sessions = {}
  new_not_paired_sessions = 0
  new_paired_sessions = 0
  paired = 0
  not_paired = 0


  Dir['/proc/*/cmdline'].map do |f|
    if File.read(f) =~ /^tmate-ssh-server \[(.+)\] \((.+)\) (.+)$/
      token = $1
      role = $2
      ip = $3

      sessions[token] ||= []
      sessions[token] << ip
    end
  end

  sessions.map do |token, ips|
    if ips.uniq.count > 1
      new_not_paired_sessions += 1 unless seen_not_paired_tokens.include?(token)
      seen_not_paired_tokens << token
      new_paired_sessions += 1 unless seen_paired_tokens.include?(token)
      seen_paired_tokens << token

      paired += 1
    else
      new_not_paired_sessions += 1 unless seen_not_paired_tokens.include?(token)
      seen_not_paired_tokens << token

      not_paired += 1
    end
  end

  StatsD.increment("tmate.#{hostname}.sessions.not-paired.total", new_not_paired_sessions)
  StatsD.increment("tmate.#{hostname}.sessions.paired.total", new_paired_sessions)
  StatsD.gauge("tmate.#{hostname}.sessions.paired", paired)
  StatsD.gauge("tmate.#{hostname}.sessions.not-paired", not_paired)

  sleep 10
end