fauna / twist

A Ruby script for logging your system events to Twitter

This URL has Read+Write access

twist / twist.rb
100755 58 lines (50 sloc) 1.584 kb
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
#!/usr/bin/env ruby
# Twist - log your system events to twitter
# Copyright 2007 Cloudburst, LLC
# Licensed under the AFL 3. See the included LICENSE file.
 
require 'yaml'
config = YAML.load_file "/etc/twist.yml"
#---
#:sysuser: httpd
#:user: twitter_user
#:password: twitter_password
 
LOG = "/var/log/auth.log"
 
puts "already running" or exit if `ps awx | grep ruby | grep twist | wc`.to_i > 2
puts "twittering..."
 
require "rubygems"
require "twitter"
 
twit = Twitter::Base.new config[:user], config[:password]
tail = "tail -n 300 #{LOG} | grep -v '(pam_unix)'" # optionally filter some events
tmplog = "/tmp/twist.log"
 
last_msg = open(tmplog).read if File.exist?(tmplog)
if last_msg
  msgs = `#{tail}`.split("\n")
  if msgs.include?(last_msg)
    msgs = msgs[msgs.index(last_msg)+1..-1]
    puts "found last message"
  else
    puts "couldn't find last message #{last_msg.inspect}"
  end
  msgs.each do |n|
    last_msg = n
    case n = n.sub(/.*? .*? .*? /, '')
      when /allison sudo/
        if n =~ /allison sudo: (.*?) :.*PWD=(.*?) ; USER=(.*?) ; COMMAND=(.*)/
          n = "#$1->#$3 $ sudo #$4"
        end
      when /Accepted publickey for/
        if n =~ /publickey for (.*?) from (.*?) port (.*?) (.*)/
          n = "#$4 by #$1 from #$2"
        end
    end
    twit.update n
    puts "twittered #{n[0..55]}..."
  end
else
  n = "#{tmplog.inspect} not found at #{Time.now}; restarted?"
  twit.update n
  puts "twittered #{n[0..55]}..."
  last_msg = `#{tail}`.split("\n").last
end
 
open(tmplog, 'w').write(last_msg)
puts "wrote #{last_msg.inspect} to #{tmplog}"