Skip to content

Commit

Permalink
Add recipe for deploying Alertmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Magrath committed Aug 5, 2015
1 parent 21141d6 commit 921e933
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
}

chef.run_list = [
'recipe[prometheus::default]'
'recipe[prometheus::default]',
'recipe[prometheus::alertmanager]'
]
end
end
34 changes: 34 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,37 @@

# Path to static asset directory, available at /user.
default['prometheus']['flags']['web.user-assets'] = ''

# Location of Alertmanager binary
default['prometheus']['alertmanager']['binary'] = "#{node['prometheus']['dir']}/alertmanager"

# Alertmanager version to build
default['prometheus']['alertmanager']['version'] = '0.0.4'

# Alertmanager source repository.
default['prometheus']['alertmanager']['git_repository'] = 'https://github.com/prometheus/alertmanager.git'

# Alertmanager source repository git reference. Defaults to version tag. Can
# also be set to a branch or master.
default['prometheus']['alertmanager']['git_revision'] = node['prometheus']['alertmanager']['version']

# Alertmanager configuration file name.
default['prometheus']['alertmanager']['config.file'] = "#{node['prometheus']['dir']}/alertmanager.conf"

# Alertmanager configuration chef template name.
default['prometheus']['alertmanager']['config_cookbook_name'] = 'prometheus'

# Alertmanager custom configuration cookbook. Use this if you'd like to bypass the
# default prometheus cookbook Alertmanager configuration template and implement your own
# templates and recipes to configure Alertmanager.
default['prometheus']['alertmanager']['config_template_name'] = 'alertmanager.conf.erb'

# Service key to use when Alertmanager notifies Pager Duty
default['prometheus']['alertmanager']['pagerduty_service_key'] = 'supersecretapikey'

# Auth token to use when Alertmanager notifies HipChat
default['prometheus']['alertmanager']['hipchat_auth_token'] = 'hipchatauthtoken'

# Room ID to use when Alertmanager notifies HipChat
default['prometheus']['alertmanager']['hipchat_room_id'] = 123456

93 changes: 93 additions & 0 deletions recipes/alertmanager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# Cookbook Name:: prometheus
# Recipe:: alertmanager
#
# Author: Paul Magrath <paul@paulmagrath.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

include_recipe 'build-essential::default'

user node['prometheus']['user'] do
system true
shell '/bin/false'
home node['prometheus']['dir']
not_if { node['prometheus']['use_existing_user'] == true || node['prometheus']['user'] == 'root' }
end

directory node['prometheus']['dir'] do
owner node['prometheus']['user']
group node['prometheus']['group']
mode '0755'
recursive true
end

directory node['prometheus']['log_dir'] do
owner node['prometheus']['user']
group node['prometheus']['group']
mode '0755'
recursive true
end

# -- Write our Config -- #

template node['prometheus']['alertmanager']['config.file'] do
cookbook node['prometheus']['alertmanager']['config_cookbook_name']
source node['prometheus']['alertmanager']['config_template_name']
mode 0644
owner node['prometheus']['user']
group node['prometheus']['group']
notifies :restart, 'service[alertmanager]'
end

# -- Do the install -- #

# These packages are needed go build
%w( curl git-core mercurial gzip sed ).each do |pkg|
package pkg
end

git "#{Chef::Config[:file_cache_path]}/alertmanager-#{node['prometheus']['alertmanager']['version']}" do
repository node['prometheus']['alertmanager']['git_repository']
revision node['prometheus']['alertmanager']['git_revision']
action :checkout
end

bash 'compile_alertmanager_source' do
cwd "#{Chef::Config[:file_cache_path]}/alertmanager-#{node['prometheus']['alertmanager']['version']}"
code <<-EOH
make &&
mv alertmanager #{node['prometheus']['dir']}
EOH

notifies :restart, 'service[alertmanager]'
end

template '/etc/init/alertmanager.conf' do
source 'upstart/alertmanager.service.erb'
mode 0644
notifies :restart, 'service[alertmanager]', :delayed
end

service 'alertmanager' do
provider Chef::Provider::Service::Upstart
action [:enable, :start]
end

# rubocop:disable Style/HashSyntax
service 'alertmanager' do
supports :status => true, :restart => true
end
# rubocop:enable Style/HashSyntax

14 changes: 14 additions & 0 deletions templates/default/alertmanager.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
notification_config {
name: "alertmanager_pagerduty"
pagerduty_config: {
service_key: "<%= node['prometheus']['alertmanager']['pagerduty_service_key'] %>"
}
}

notification_config {
name: "alertmanager_hipchat"
hipchat_config: {
auth_token: "<%= node['prometheus']['alertmanager']['hipchat_auth_token'] %>"
room_id: <%= node['prometheus']['alertmanager']['hipchat_room_id'] %>
}
}
14 changes: 14 additions & 0 deletions templates/default/upstart/alertmanager.service.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description "Prometheus Alertmanager"
start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [016]

respawn
env GOMAXPROCS=<%= node['cpu']['total'] %>
setuid <%= node['prometheus']['user'] %>
setgid <%= node['prometheus']['user'] %>

script
exec >> "<%= node['prometheus']['log_dir'] %>/alertmanager.log"
exec 2>&1
exec <%= node['prometheus']['alertmanager']['binary'] %> -config.file=<%= node['prometheus']['alertmanager']['config.file'] %>
end script

0 comments on commit 921e933

Please sign in to comment.