-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhipchat_notify.rb
executable file
·93 lines (83 loc) · 2.5 KB
/
hipchat_notify.rb
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
#!/usr/bin/ruby
require 'rubygems'
require 'hipchat-api'
require 'getopt/long'
require 'socket'
require 'erb'
require 'yaml'
FILENAME='/usr/local/etc/hipchat_notify.yaml'
data = YAML::load(File.open(FILENAME))
HipToken=data["token"]
RoomID=data["roomid"]
FromID=data["from"]
Colors=data["colors"]
#ERB templates for message format
$types={
'host'=>
%q{ <%= @timestamp %> - Origin: nagios@<%= @nagioshost %>
Notification type: <%= @type %>
Host: <%= @hostname %> (Address <%= @hostaddress %>)
<%= @hostoutput %>
}.gsub(/\n/,'<br>'),
'service'=>
%q{ <%= @timestamp %> - Origin: nagios@<%= @nagioshost %>
Notification type: <%= @type %>
Host: <%= @hostalias %> (Address <%= @hostaddress %>)
<%= @serviceoutput %>
}.gsub(/\n/,'<br>')
}
#Locate room id. - save time - use previously located id
def getroomid(hipconn,roomname)
roomid=nil
roomid if hipconn.nil? || roomname.nil?
hipconn.rooms_list['rooms'].each do |thisroom|
roomid=thisroom['room_id'] if thisroom['name'] == roomname
end
roomid
end
def getuserid(hipconn,username)
userid=nil
hipconn.users_list['users'].each do |thisuser|
userid=thisuser['user_id'] if thisuser['name']==username
end
userid
end
#'$SERVICESTATE$|$STATETYPE$|$HOSTSTATE$|$SERVICEDESC$|$OUTPUT$|$SHORTDATETIME$|$HOSTNAME$'
$opts=Getopt::Long.getopts(
["--type","-t",Getopt::REQUIRED],
["--inputs","-i",Getopt::REQUIRED],
["--notify","-n",Getopt::BOOLEAN]
)
if(! $types.has_key?( $opts['type'] ) )
$stderr.puts "Unknown notification type: #{$opts['type']}!"
exit
end
msg=nil
whichcolor='gray'
if($opts['type'] == 'host')
@nagioshost=Socket.gethostname.split('.')[0]
@hostname,@timestamp,@type,@hostaddress,@hoststate,@hostoutput = $opts['inputs'].split('|')
msg=ERB.new($types[ $opts['type'] ]).result()
whichcolor=Colors[@type] || 'gray'
elsif ($opts['type'] == 'service')
@nagioshost=Socket.gethostname.split('.')[0]
@servicedesc,@hostalias,@timestamp,@type,@hostaddress,@servicestate,@serviceoutput = $opts['inputs'].split('|')
msg=ERB.new($types[ $opts['type'] ]).result()
if @type == 'PROBLEM'
if @servicestate == 'WARNING'
whichcolor = 'yellow'
elsif @servicestate == 'CRITICAL'
whichcolor = 'red'
end
else
whichcolor=Colors[@type] || 'gray'
end
end
conn=nil
begin
conn=HipChat::API.new(HipToken)
rescue Exception => e
$stderr.puts "Error connecting to HipChat: "+e.inspect
exit
end
conn.rooms_message(RoomID,FromID,msg,notify = $opts['notify'],color= whichcolor)