├── CONTRIBUTORS ├── test └── support │ └── Gemfile.ci ├── .travis.yml ├── templates └── default │ ├── config.js.erb │ ├── statsd.conf.erb │ └── statsd.erb ├── metadata.rb ├── attributes └── default.rb ├── CHANGELOG.md ├── Rakefile ├── README.md ├── files └── default │ └── tests │ └── minitest │ └── support │ └── statsd_test.rb └── recipes └── default.rb /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | Chris Lundquist (@ChrisLundquist) 2 | Nelson Chen (@crazysim) 3 | -------------------------------------------------------------------------------- /test/support/Gemfile.ci: -------------------------------------------------------------------------------- 1 | source :rubygems 2 | 3 | gem "rake" 4 | gem "foodcritic" 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | gemfile: 3 | - test/support/Gemfile.ci 4 | rvm: 5 | - 1.9.2 6 | - 1.9.3 7 | script: BUNDLE_GEMFILE=test/support/Gemfile.ci bundle exec rake foodcritic 8 | -------------------------------------------------------------------------------- /templates/default/config.js.erb: -------------------------------------------------------------------------------- 1 | { 2 | "graphitePort": <%= @graphite_port %>, 3 | "graphiteHost": "<%= @graphite_host %>", 4 | "address": "<%= @address %>", 5 | "port": <%= @port %>, 6 | "flushInterval": <%= @flush_interval %> 7 | } 8 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | maintainer "Hector Castro" 2 | maintainer_email "hectcastro@gmail.com" 3 | license "Apache 2.0" 4 | description "Installs and configures StatsD." 5 | version "0.0.10" 6 | recipe "statsd", "Installs and configures StatsD" 7 | name "statsd" 8 | 9 | %w{ git logrotate nodejs }.each do |d| 10 | depends d 11 | end 12 | 13 | %w{ ubuntu rhel scientific redhat centos amazon}.each do |os| 14 | supports os 15 | end 16 | 17 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | default["statsd"]["dir"] = "/usr/share/statsd" 2 | default["statsd"]["conf_dir"] = "/etc/statsd" 3 | default["statsd"]["repository"] = "git://github.com/etsy/statsd.git" 4 | default["statsd"]["log_file"] = "/var/log/statsd.log" 5 | default["statsd"]["flush_interval"] = 10000 6 | default["statsd"]["address"] = "0.0.0.0" 7 | default["statsd"]["port"] = 8125 8 | default["statsd"]["graphite_host"] = "localhost" 9 | default["statsd"]["graphite_port"] = 2003 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v0.0.10 2 | 3 | * Removed trailing comma that was causing issues with Chef 10.18.2. 4 | 5 | ## v0.0.9 6 | 7 | * Fixed service resource restart support. 8 | 9 | ## v0.0.8 10 | 11 | * Added support for RHEL. 12 | 13 | ## v0.0.7 14 | 15 | * Fixed order of shell redirection to ensure that `STDERR` flows to log file. 16 | 17 | ## v0.0.6 18 | 19 | * Added init script support for Ubuntu 10.04 and Upstart 0.6.5. 20 | 21 | ## v0.0.5 22 | 23 | * Altered Upstart configuration file for version 1.4 and above. 24 | 25 | ## v0.0.4 26 | 27 | * Added Foodcritic and Travis CI. 28 | -------------------------------------------------------------------------------- /templates/default/statsd.conf.erb: -------------------------------------------------------------------------------- 1 | description "StatsD" 2 | author "Chef" 3 | 4 | start on (filesystem and net-device-up) 5 | stop on runlevel [!2345] 6 | 7 | respawn 8 | respawn limit 5 30 9 | 10 | chdir /usr/share/statsd 11 | <% unless @platform_version < 11.10 -%> 12 | setuid statsd 13 | <% end %> 14 | 15 | script 16 | <% if @platform_version < 11.10 -%> 17 | exec start-stop-daemon --start --chuid statsd --exec node /usr/share/statsd/stats.js /etc/statsd/config.js >> <%= @log_file %> 2>&1 18 | <% else -%> 19 | exec node /usr/share/statsd/stats.js /etc/statsd/config.js >> <%= @log_file %> 2>&1 20 | <% end -%> 21 | end script 22 | 23 | emits statsd-running 24 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | desc "Runs foodcritic" 2 | task :foodcritic do 3 | if Gem::Version.new("1.9.2") <= Gem::Version.new(RUBY_VERSION.dup) 4 | sandbox = File.join(File.dirname(__FILE__), %w{tmp foodcritic cookbook}) 5 | prepare_foodcritic_sandbox(sandbox) 6 | 7 | sh "foodcritic --epic-fail any #{File.dirname(sandbox)}" 8 | else 9 | STDERR.puts "WARN: foodcritic run is skipped as Ruby #{RUBY_VERSION} is < 1.9.2." 10 | end 11 | end 12 | 13 | task :default => "foodcritic" 14 | 15 | private 16 | 17 | def prepare_foodcritic_sandbox(sandbox) 18 | files = %w{*.md *.rb attributes definitions files providers 19 | recipes resources templates} 20 | 21 | rm_rf sandbox 22 | mkdir_p sandbox 23 | cp_r Dir.glob("{#{files.join(',')}}"), sandbox 24 | puts "\n\n" 25 | end 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # statsd [![Build Status](https://secure.travis-ci.org/hectcastro/chef-statsd.png?branch=master)](http://travis-ci.org/hectcastro/chef-statsd) 2 | 3 | ## Description 4 | 5 | Installs and configures StatsD. 6 | 7 | ## Requirements 8 | 9 | ### Platforms 10 | 11 | * Ubuntu 11.10 (Oneiric) 12 | * Ubuntu 12.04 (Precise) 13 | * CentOS 6.3 (Final) 14 | 15 | ### Cookbooks 16 | 17 | * git 18 | * logrotate 19 | * nodejs 20 | 21 | ## Attributes 22 | 23 | * `node["statsd"]["dir"]` - Directory to install into. 24 | * `node["statsd"]["conf_dir"]` - Directory for StatsD configuration. 25 | * `node["statsd"]["repository"]` - Reference to a StatsD repository. 26 | * `node["statsd"]["log_file"]` - Path to the StatsD log file. 27 | * `node["statsd"]["flush_interval"]` - Flush interval in milliseconds. 28 | * `node["statsd"]["address"]` - Address to bind StatsD to. 29 | * `node["statsd"]["port"]` - Port to run StatsD on. 30 | * `node["statsd"]["graphite_host"]` - Graphite host. 31 | * `node["statsd"]["graphite_port"]` - Graphite port. 32 | 33 | ## Recipes 34 | 35 | * `recipe[statsd]` will install StatsD. 36 | 37 | ## Usage 38 | 39 | Currently only supports installation via a Git repository. 40 | -------------------------------------------------------------------------------- /files/default/tests/minitest/support/statsd_test.rb: -------------------------------------------------------------------------------- 1 | require "minitest/autorun" 2 | require "socket" 3 | 4 | describe_recipe "statsd::default" do 5 | include MiniTest::Chef::Assertions 6 | include MiniTest::Chef::Context 7 | include MiniTest::Chef::Resources 8 | 9 | describe "files" do 10 | it "creates the share directory" do 11 | directory(node["statsd"]["dir"]).must_exist 12 | end 13 | 14 | it "creates the configuration directory" do 15 | directory(node["statsd"]["conf_dir"]).must_exist 16 | end 17 | 18 | it "creates the configuration file" do 19 | file("#{node["statsd"]["conf_dir"]}/config.js").must_exist 20 | end 21 | 22 | it "creates the Upstart script" do 23 | file("/etc/init/statsd.conf").must_exist 24 | end 25 | 26 | it "creates the log file" do 27 | file(node["statsd"]["log_file"]).must_exist 28 | end 29 | end 30 | 31 | describe "users" do 32 | it "creates a daemon user" do 33 | user("statsd").must_exist 34 | end 35 | end 36 | 37 | describe "services" do 38 | it "runs as a daemon" do 39 | service("statsd").must_be_running 40 | end 41 | 42 | it "accepts connections" do 43 | s = TCPSocket.new("127.0.0.1", 8126) 44 | s.puts("help\r\n") 45 | help = s.gets 46 | s.close 47 | 48 | assert(help =~ /Commands:/) 49 | end 50 | end 51 | end 52 | 53 | 54 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | include_recipe "git" 2 | include_recipe "nodejs" 3 | include_recipe "logrotate" 4 | 5 | git node["statsd"]["dir"] do 6 | repository node["statsd"]["repository"] 7 | action :sync 8 | notifies :restart, "service[statsd]" 9 | end 10 | 11 | directory node["statsd"]["conf_dir"] do 12 | action :create 13 | end 14 | 15 | template "#{node["statsd"]["conf_dir"]}/config.js" do 16 | mode "0644" 17 | source "config.js.erb" 18 | variables( 19 | :address => node["statsd"]["address"], 20 | :port => node["statsd"]["port"], 21 | :flush_interval => node["statsd"]["flush_interval"], 22 | :graphite_port => node["statsd"]["graphite_port"], 23 | :graphite_host => node["statsd"]["graphite_host"] 24 | ) 25 | notifies :restart, "service[statsd]" 26 | end 27 | 28 | 29 | case node["platform_family"] 30 | when "debian" 31 | template "/etc/init/statsd.conf" do 32 | mode "0644" 33 | source "statsd.conf.erb" 34 | variables( 35 | :log_file => node["statsd"]["log_file"], 36 | :platform_version => node["platform_version"].to_f 37 | ) 38 | end 39 | when "rhel","fedora" 40 | template "/etc/init.d/statsd" do 41 | mode "0755" 42 | source "statsd.erb" 43 | variables( 44 | :log_file => node["statsd"]["log_file"] 45 | ) 46 | end 47 | end 48 | 49 | user "statsd" do 50 | system true 51 | shell "/bin/false" 52 | end 53 | 54 | file node["statsd"]["log_file"] do 55 | owner "statsd" 56 | action :create 57 | end 58 | 59 | logrotate_app "statsd" do 60 | cookbook "logrotate" 61 | path node["statsd"]["log_file"] 62 | frequency "daily" 63 | rotate 7 64 | create "644 root root" 65 | end 66 | 67 | service "statsd" do 68 | case node["platform"] 69 | when "ubuntu" 70 | if node["platform_version"].to_f >= 9.10 71 | provider Chef::Provider::Service::Upstart 72 | end 73 | #restart_command "sudo service statsd stop && sudo service statsd start" 74 | end 75 | action [ :enable, :start ] 76 | supports :start => true, :stop => true, :restart => true, :status => true 77 | end 78 | -------------------------------------------------------------------------------- /templates/default/statsd.erb: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # /etc/rc.d/init.d/statsd 4 | # 5 | # Starts the statsd daemon 6 | # 7 | # chkconfig: 2345 20 80 8 | # description: Frontend aggregatation of messages destined for graphite daemon 9 | # processname: statsd 10 | 11 | ### BEGIN INIT INFO 12 | # Provides: statsd 13 | # Defalt-Start: 2 3 4 5 14 | # Default-Stop: 0 1 6 15 | # Description: Frontend aggregatation of messages destined for graphite daemon 16 | ### END INIT INFO 17 | 18 | 19 | 20 | # Source function library. 21 | . /etc/rc.d/init.d/functions 22 | 23 | NAME=statsd 24 | INSTALL_DIR=/usr/share/$NAME 25 | NODE_EXE=/usr/local/bin/node 26 | 27 | [ -x $NODE_EXE ] || exit 0 28 | [ -f $INSTALL_DIR/stats.js ] || exit 0 29 | 30 | RETVAL=0 31 | 32 | # 33 | # See how we were called. 34 | # 35 | 36 | start() { 37 | # Check if it is already running 38 | if [ ! -f /var/lock/subsys/$NAME ]; then 39 | echo -n $"Starting $NAME daemon: " 40 | # daemon 41 | sudo -u statsd -- $NODE_EXE $INSTALL_DIR/stats.js /etc/$NAME/config.js > <%= @log_file %> 2>&1 < <%= @log_file %> & 42 | RETVAL=$? 43 | if [ $RETVAL -eq 0 ]; then 44 | touch /var/lock/subsys/$NAME 45 | echo_success 46 | else 47 | echo_failure 48 | fi 49 | echo 50 | fi 51 | return $RETVAL 52 | } 53 | 54 | stop() { 55 | echo -n $"Stopping $NAME daemon: " 56 | pid=$(ps aux | grep stats.js | grep nodejs | awk '{print $2}') 57 | kill $pid 58 | RETVAL=$? 59 | if [ $RETVAL -eq 0 ]; then 60 | rm -f /var/lock/subsys/$NAME 61 | echo_success 62 | else 63 | echo_failure 64 | fi 65 | echo 66 | return $RETVAL 67 | } 68 | 69 | 70 | restart() { 71 | stop 72 | start 73 | } 74 | 75 | reload() { 76 | trap "" SIGHUP 77 | killall -HUP stats.js 78 | } 79 | 80 | case "$1" in 81 | start) 82 | start 83 | ;; 84 | stop) 85 | stop 86 | ;; 87 | reload) 88 | reload 89 | ;; 90 | restart) 91 | restart 92 | ;; 93 | condrestart) 94 | if [ -f /var/lock/subsys/$NAME ]; then 95 | restart 96 | fi 97 | ;; 98 | status) 99 | if [ -f /var/lock/subsys/$NAME ]; then 100 | echo "$NAME is running" 101 | exit 0 102 | else 103 | echo "$NAME is stopped" 104 | exit 3 105 | fi 106 | ;; 107 | *) 108 | echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}" 109 | exit 1 110 | esac 111 | 112 | exit $RETVAL 113 | --------------------------------------------------------------------------------