diff --git a/cookbooks/main/recipes/default.rb b/cookbooks/main/recipes/default.rb index 0d7c6429..51362800 100644 --- a/cookbooks/main/recipes/default.rb +++ b/cookbooks/main/recipes/default.rb @@ -138,3 +138,6 @@ # postgresql9_pg_buffercache "postgres" # postgresql9_pg_freespacemap "postgres" #end + +# uncomment to include the StatsD-Librato recipe +# require_recipe "statsd-librato" diff --git a/cookbooks/statsd-librato/README.md b/cookbooks/statsd-librato/README.md new file mode 100644 index 00000000..1cb55ccd --- /dev/null +++ b/cookbooks/statsd-librato/README.md @@ -0,0 +1,27 @@ +# StatsD with Librato Metrics backend + +## Overview + +This is a simple chef recipe that installs [StatsD](https://github.com/etsy/statsd) with [Librato Metrics](https://metrics.librato.com) backend. + +## Requirements + +* An active [Librato Metrics](https://metrics.librato.com/sign_up) account. + +## Configuration + +* Set your [Librato Metrics](https://metrics.librato.com) credentials in ```cookbooks/recipes/defatult.rb```. + +```ruby +librato_email = "you@example.com" +librato_token = "your-token-goes-here" +``` + +## Enabling + +* Enable the recipe in ```cookbooks/main/recipes/default.rb```. + +```ruby +# uncomment to include the StatsD-Librato recipe +require_recipe "statsd-librato" +``` diff --git a/cookbooks/statsd-librato/recipes/default.rb b/cookbooks/statsd-librato/recipes/default.rb new file mode 100644 index 00000000..4ad3f527 --- /dev/null +++ b/cookbooks/statsd-librato/recipes/default.rb @@ -0,0 +1,73 @@ +# +# Cookbook Name:: statsd-librato +# Recipe:: default +# + +# Set your Librato credentials +librato_email = "you@example.com" +librato_token = "your-token-goes-here" + +# Set your app name +app_name = "your-app-name-goes-here" + +# Set install dir for statsd +src_dir = "/usr/lib/statsd" + +statsd_port = 8126 + +statsd_enabled_env = ['production'].include?(node[:environment][:framework_env]) +statsd_enabled_instance = ['app_master', 'app'].include?(node[:instance_role]) + +if statsd_enabled_env && statsd_enabled_instance + execute "install statsd" do + command "git clone git://github.com/etsy/statsd.git" + cwd "/usr/lib" + not_if { File.exists?("#{src_dir}/.git/")} + user "root" + end + + execute "install statsd-librato-backend" do + command "npm install statsd-librato-backend" + cwd "/usr/lib/statsd" + not_if { File.exists?("#{src_dir}/node_modules/statsd-librato-backend/package.json")} + user "root" + end + + template "/data/#{app_name}/shared/config/statsd.js" do + owner node[:owner_name] + group node[:owner_name] + mode 0644 + source "config.js.erb" + variables({ + :statsd_port => port, + :librato_email => librato_email, + :librato_token => librato_token + }) + end + + template "/data/#{app_name}/shared/statsd" do + owner node[:owner_name] + group node[:owner_name] + mode 0744 + source "statsd.erb" + variables({ + :src_dir => src_dir, + :app_name => app_name + }) + end + + template "/etc/monit.d/statsd.monitrc" do + owner node[:owner_name] + group node[:owner_name] + mode 0644 + source "monitrc.conf.erb" + variables({ + :user => node[:owner_name], + :app_name => app_name + }) + end + + execute "monit reload" do + action :run + end +end diff --git a/cookbooks/statsd-librato/templates/default/config.js.erb b/cookbooks/statsd-librato/templates/default/config.js.erb new file mode 100644 index 00000000..b5439b7a --- /dev/null +++ b/cookbooks/statsd-librato/templates/default/config.js.erb @@ -0,0 +1,8 @@ +{ + port: <%= @statsd_port %>, + backends: [ "statsd-librato-backend" ], + librato: { + email: "<%= @librato_email %>", + token: "<%= @librato_token %>" + } +} diff --git a/cookbooks/statsd-librato/templates/default/monitrc.conf.erb b/cookbooks/statsd-librato/templates/default/monitrc.conf.erb new file mode 100644 index 00000000..7685ce4e --- /dev/null +++ b/cookbooks/statsd-librato/templates/default/monitrc.conf.erb @@ -0,0 +1,5 @@ +check process statsd + with pidfile /data/<%= @app_name %>/shared/pids/statsd.pid + start = "/data/<%= @app_name %>/shared/statsd start" as uid <%= @user %> and gid <%= @user %> + stop = "/data/<%= @app_name %>/shared/statsd stop" as uid <%= @user %> and gid <%= @user %> + group statsd diff --git a/cookbooks/statsd-librato/templates/default/statsd.erb b/cookbooks/statsd-librato/templates/default/statsd.erb new file mode 100644 index 00000000..2871e738 --- /dev/null +++ b/cookbooks/statsd-librato/templates/default/statsd.erb @@ -0,0 +1,14 @@ +#!/bin/bash + +case $1 in + start) + echo $$ > /data/<%= @app_name %>/shared/pids/statsd.pid; + exec 2>&1 node <%= @src_dir %>/stats.js /data/<%= @app_name %>/shared/config/statsd.js + ;; + stop) + kill `cat /data/<%= @app_name %>/shared/pids/statsd.pid` ;; + *) + echo "usage: statsd {start|stop}" ;; + +esac +exit 0