Skip to content

Commit

Permalink
Merge pull request #33 from rackspace-cookbooks/master
Browse files Browse the repository at this point in the history
Added cent/rhel support for agent
  • Loading branch information
philk committed Jul 25, 2013
2 parents 2f7d60c + 95a92a4 commit 925d7f7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 69 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Specifically this recipe will focus on main atom's in the system.
* Entities
* Checks
* Alarms
* Agents (Ubuntu only currently)
* Agents
* Agent Tokens

The cookbook also installs the python-pip package in Debian and RedHat based systems, and then uses pip to install the Rackspace Cloud Monitoring client, raxmon-cli, via pip
Expand Down Expand Up @@ -291,10 +291,25 @@ The Agent recipe will install the cloud monitoring agent on your node and either
or if none is provided it will call the cloud_monitoring_agent_token provider to generate a new one for this node.

The Agent token can either be provided through the following attribute.
node['cloud_monitoring']['agent']['token']
* `node['cloud_monitoring']['agent']['token']`

or through an entry in the rackspace cloud data bag like so.
"token": "<Your Agent Token>"

* `"token": "<Your Agent Token>"`


Example Agent check

```ruby
cloud_monitoring_check "cpu" do
type 'agent.cpu'
period 30
timeout 10
rackspace_username node['cloud_monitoring']['rackspace_username']
rackspace_api_key node['cloud_monitoring']['rackspace_api_key']
action :create
end
```

## Agent Plugins

Expand Down
32 changes: 0 additions & 32 deletions files/default/signing-key.asc

This file was deleted.

125 changes: 92 additions & 33 deletions recipes/agent.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
case node['platform']
when "ubuntu","debian"

cookbook_file "#{Chef::Config[:file_cache_path]}/signing-key.asc" do
source "signing-key.asc"
mode 0755
owner "root"
group "root"
end
when "ubuntu", "debian"

apt_repository "cloud-monitoring" do
uri "http://stable.packages.cloudmonitoring.rackspace.com/ubuntu-10.04-x86_64"

if node['platform'] == 'ubuntu'
uri "http://stable.packages.cloudmonitoring.rackspace.com/ubuntu-#{node['platform_version']}-#{node['kernel']['machine']}"
elsif node['platform'] =='debian'
uri "http://stable.packages.cloudmonitoring.rackspace.com/debian-#{node['lsb']['codename']}-#{node['kernel']['machine']}"
end

distribution "cloudmonitoring"
components ["main"]
key "signing-key.asc"
key "https://monitoring.api.rackspacecloud.com/pki/agent/linux.asc"
action :add
end


when "redhat","centos","fedora", "amazon","scientific"

#Grab the major release for cent and rhel servers as this is what the repos use.
releaseVersion = node['platform_version'].split('.').first

#We need to figure out which signing key to use, cent5 and rhel5 have their own.
if (node['platform'] == 'centos') && (releaseVersion == '5')
signingKey = 'https://monitoring.api.rackspacecloud.com/pki/agent/centos-5.asc'
elsif (node['platform'] == 'redhat') && (releaseVersion == '5')
signingKey = 'https://monitoring.api.rackspacecloud.com/pki/agent/redhat-5.asc'
else
signingKey = 'https://monitoring.api.rackspacecloud.com/pki/agent/linux.asc'
end

yum_key "Rackspace-Monitoring" do
url signingKey
action :add
end

yum_repository "cloud-monitoring" do
description "Rackspace Monitoring"
url "http://stable.packages.cloudmonitoring.rackspace.com/#{node['platform']}-#{releaseVersion}-#{node['kernel']['machine']}"
action :add
end

end
# TODO: Enable once we set it up
# apt_repository "cloud-monitoring-release" do
# uri "http://stable.packages.cloudmonitoring.rackspace.com/linux-x86_64-ubuntu-10.04"
# distribution "cloudmonitoring"
# components ["main"]
# key "signing-key.asc"
# action :add
#end

begin
databag_dir = node["cloud_monitoring"]["credentials"]["databag_name"]
Expand All @@ -37,24 +54,62 @@
Chef::Log.error 'Failed to load rackspace cloud data bag: ' + e.to_s
end

if not node['cloud_monitoring']['agent']['token']

if not node['cloud_monitoring']['rackspace_username'] or not node['cloud_monitoring']['rackspace_api_key']
raise RuntimeError, "agent_token variable or rackspace credentials must be set on the node."
#The first time this recipe runs on the node it is unable to pull token from the node attributes, unless they were put there by hand or in the data bag.
#There's also no simple way to get data directly back from a provider.
#So we're creating the auth_token with the LWRP, then using fog to pull it back out of the API. If you can find a better way to handle this, please rewrite it.
if node['cloud_monitoring']['agent']['token'].nil?

if node['cloud_monitoring']['rackspace_username'] == "your_rackspace_username" or node['cloud_monitoring']['rackspace_api_key'] == "your_rackspace_api_key"
raise RuntimeError, "No Rackspace credentials found"

#This runs at compile time as it needs to finish before the template for the config file fires off.
#Create the token within the api, I'm using run_action to make sure everything happens in the proper order.
else
e = cloud_monitoring_agent_token "#{node.hostname}" do
create_token = cloud_monitoring_agent_token "#{node.hostname}" do
rackspace_username node['cloud_monitoring']['rackspace_username']
rackspace_api_key node['cloud_monitoring']['rackspace_api_key']
action :nothing
end
e.run_action(:create)

create_token.run_action(:create)

#Pull just the token itself into a variable named token
label = "#{node.hostname}"
monitoring = Fog::Monitoring::Rackspace.new(:rackspace_api_key => node['cloud_monitoring']['rackspace_api_key'], :rackspace_username => node['cloud_monitoring']['rackspace_username'])
tokens = Hash[monitoring.agent_tokens.all.map {|x| [x.label, x]}]
possible = tokens.select {|key, value| value.label === label}
possible = Hash[*possible.flatten(1)]

if !possible.empty? then
possible.values.first
else
nil
end

token = possible[label].token

#Fire off a template run using the token pulled out of fog. This should only ever run on a new node, or if your node attributes get lost.
config_template = template "/etc/rackspace-monitoring-agent.cfg" do
source "rackspace-monitoring-agent.erb"
owner "root"
group "root"
mode 0600
variables(
:monitoring_id => "#{node.hostname}",
:monitoring_token => token
)
action :nothing
end

config_template.run_action(:create)

end

end




package "rackspace-monitoring-agent" do
if node['cloud_monitoring']['agent']['version'] == 'latest'
action :upgrade
Expand All @@ -66,15 +121,17 @@
notifies :restart, "service[rackspace-monitoring-agent]"
end

template "/etc/rackspace-monitoring-agent.cfg" do
source "rackspace-monitoring-agent.erb"
owner "root"
group "root"
mode 0600
variables(
:monitoring_id => node['cloud_monitoring']['agent']['id'],
:monitoring_token => node['cloud_monitoring']['agent']['token']
)
unless node['cloud_monitoring']['agent']['token'].nil?
template "/etc/rackspace-monitoring-agent.cfg" do
source "rackspace-monitoring-agent.erb"
owner "root"
group "root"
mode 0600
variables(
:monitoring_id => node['cloud_monitoring']['agent']['id'],
:monitoring_token => node['cloud_monitoring']['agent']['token']
)
end
end

node['cloud_monitoring']['plugins'].each_pair do |source_cookbook, path|
Expand Down Expand Up @@ -106,4 +163,6 @@
end

action [ :enable, :start ]
subscribes :restart, resources(:template => '/etc/rackspace-monitoring-agent.cfg'), :delayed

end
5 changes: 4 additions & 1 deletion recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
action :nothing
end

if File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400

if !File.exists?('/var/lib/apt/periodic/update-success-stamp')
apt.run_action(:run)
elsif File.mtime('/var/lib/apt/periodic/update-success-stamp') < Time.now - 86400
apt.run_action(:run)
end

Expand Down

0 comments on commit 925d7f7

Please sign in to comment.