Skip to content

Instantly share code, notes, and snippets.

@sax
Forked from matthiasg/carbon-cache.sh
Last active December 29, 2015 11:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sax/7663192 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple Ad Hoc Carbon Cache Service
#
# put in /opt/custom/share/svc/carbon-cache.sh
set -o xtrace
. /lib/svc/share/smf_include.sh
cd /
PATH=/usr/sbin:/usr/bin:/opt/custom/bin:/opt/custom/sbin; export PATH
case "$1" in
'start')
#### STARTUP
/opt/local/bin/carbon-cache.py --config=/opt/local/etc/graphite/carbon.conf start
;;
'stop')
#### SHUTDOWN
/opt/local/bin/carbon-cache.py --config=/opt/local/etc/graphite/carbon.conf stop
;;
*)
echo "Usage: $0 { start | stop }"
exit $SMF_EXIT_ERR_FATAL
;;
esac
exit $SMF_EXIT_OK
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<!-- put in /opt/custom/smf and import using svccfg import /opt/custom/smf/carbon-cache.xml -->
<service_bundle type='manifest' name='export'>
<service name='graphite/carbon-cache' type='service' version='0'>
<create_default_instance enabled='true'/>
<single_instance/>
<dependency name='net-physical' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/network/physical'/>
</dependency>
<dependency name='filesystem' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/local'/>
</dependency>
<exec_method name='start' type='method' exec='/opt/custom/share/svc/carbon-cache.sh %m' timeout_seconds='0'/>
<exec_method name='stop' type='method' exec='/opt/custom/share/svc/carbon-cache.sh %m' timeout_seconds='60'/>
<property_group name='startd' type='framework'>
<propval name='duration' type='astring' value='transient'/>
</property_group>
<stability value='Unstable'/>
<template>
<common_name>
<loctext xml:lang='C'>Carbon Cache Default Script</loctext>
</common_name>
</template>
</service>
</service_bundle>
#!/bin/bash
# Simple Ad Hoc Graphite Django Service
set -o xtrace
. /lib/svc/share/smf_include.sh
if [ -z "$SMF_FMRI" ]; then
SMF_FMRI="svc:/graphite/management/graphite-web:default"
fi
cd /
PATH=/usr/sbin:/usr/bin:/opt/custom/bin:/opt/custom/sbin; export PATH
case "$1" in
'start')
#### STARTUP
PYTHONPATH=/opt/local/lib/python2.7/site-packages/graphite:/opt/local/lib/python2.7
/opt/local/bin/django-admin.py runserver --pythonpath=$PYTHONPATH --settings=graphite.settings 0.0.0.0:80 &
;;
'stop')
#### SHUTDOWN
contract=`svcprop -p restarter/contract $SMF_FMRI`
smf_kill_contract ${contract} TERM
;;
*)
echo "Usage: $0 { start | stop }"
exit $SMF_EXIT_ERR_FATAL
;;
esac
exit $SMF_EXIT_OK
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='export'>
<service name='graphite/graphite' type='service' version='0'>
<create_default_instance enabled='true'/>
<single_instance/>
<dependency name='net-physical' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/network/physical'/>
</dependency>
<dependency name='filesystem' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/local'/>
</dependency>
<dependency name='carbon' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/graphite/carbon-cache'/>
</dependency>
<method_context>
<method_credential user="graphite" privileges="basic,net_privaddr" group="graphite"/>
</method_context>
<exec_method name='start' type='method' exec='/opt/custom/share/svc/graphite.sh %m' timeout_seconds='0'/>
<exec_method name='stop' type='method' exec='/opt/custom/share/svc/graphite.sh %m' timeout_seconds='60'/>
<stability value='Unstable'/>
<template>
<common_name>
<loctext xml:lang='C'>Graphite Service</loctext>
</common_name>
</template>
</service>
</service_bundle>
imgadm import 84cb7edc-3f22-11e2-8a2a-3f2a7b148699
# adjust ip and stuff in vm-graphite.json
vmadm create -f vm-graphite.json
zlogin <uuid of machine just created>
### GRAPHITE
pkgin in py27-carbon-0.9.10
pkgin in py27-graphite-web-0.9.10
cd /opt/local/lib/python2.7/site-packages/graphite
cp local_settings.py.example local_settings.py
#
# change local_settings.py:
#
# TIME_ZONE = 'UTC' # (timezone based on whatever you need-BUT smartos defaults to UTC time so this is consistent )
#
# CONF_DIR = '/opt/local/graphite/conf'
# STORAGE_DIR = '/opt/local/graphite/storage'
# CONTENT_DIR = '/opt/local/graphite/webapp/content'
#
# also comment in the DATABASE={ ... } part
# and change NAME to /opt/local/graphite/storage/graphite.db
#
#
# Note: I read on http://www.spacevatican.org/2012/9/18/using-statsd-and-graphite-from-a-rails-app/
# that
#
# "When using statsd with its default settings it’s crucial that you keep at least
# some 10 second data (the default graphite config keeps 1min resolution data only).
# If you don’t then the mismatch between statsd’s flush rate and graphite’s
# flush rate will cause you to lose data. "
#
# That means storage-schemas.conf in /opt/local/etc/graphite should be edited like this:
# Add the following to storage-schemas.conf, above the [default_1min_for_1day] section
#
# [stats]
# pattern = ^stats\..*
# retentions = 10:2160,60:10080,600:262974
#
# [stats_count]
# pattern = ^stats_counts\..*
# retentions = 10:2160,60:10080,600:262974
#
# and edit storage-aggregation.conf by adding before the default entry
#
# [stats_counts]
# pattern = ^stats_counts\.
# xFilesFactor = 0
# aggregationMethod = sum
#
# CONFIGURING CARBON
# change the carbon.conf (/opt/local/etc/graphite/carbon.conf) like this
#
# STORAGE_DIR = /opt/local/graphite/storage
# CONF_DIR = /opt/local/etc/graphite
# LOG_DIR = /opt/local/graphite/storage/log/carbon-cache
# PID_DIR = /opt/local/graphite/storage
#
# LOCAL_DATA_DIR = /opt/local/graphite/storage/whisper
#
# Initialize graphite/carbon database
cd /opt/local/graphite
export LC_ALL="en_US.UTF-8" # imporant for syncdb (otherwise the user creation will fail)
mkdir /opt/local/graphite/storage
#will ask for username and password
python /opt/local/lib/python2.7/site-packages/graphite/manage.py syncdb
# create log folder
mkdir /opt/local/graphite/storage/log/webapp
#
# NOW THE SYSTEM IS SETUP, SERVICES CAN BE STARTED MANUALLY OR YOU CAN INSTALL SMF SERVICES
#
# MANUALLY>
# STARTING CARBON
carbon-cache.py --config=/opt/local/etc/graphite/carbon.conf start
# STARTING GRAPHITE
run-graphite-devel-server.py --libs=/opt/local/lib/python2.7/site-packages/graphite /opt/local/graphite &
### Optional STATSD
# I INSTALLED IT IN A DIFFERENT MACHINE (with git clone and npm install) and then copied it over to /opt/local/statsd
# git clone https://github.com/etsy/statsd
# then copy exampleConfig.js into config.js and update ip to 127.0.0.1
cd /opt/local/statsd
node stats.js config.js &
# SERVICES>
# This is alternative above (but statsd is identical just leave out the last line to start node)
mkdir /opt/custom/smf
cd /opt/custom/smf
# PUT ALL xml files in here
mkdir /opt/custom/share/svc
cd /opt/custom/share/svc
# PUT THE .sh files in here
cd /opt/custom/smf
svccfg import carbon-cache.xml
svccfg import graphite.xml
svccfg import statsd.xml
svcsadm enable carbon-cache
svcsadm enable graphite/graphite
svcsadm enable statsd
# Services are running now. statsd logs to /var/log/statsd.log and when set
# to debug this will get huge to beware! you can change statsd.sh of course.
#!/bin/bash
# Simple Ad Hoc Graphite StatsD Service
set -o xtrace
. /lib/svc/share/smf_include.sh
cd /
PATH=/usr/sbin:/usr/bin:/opt/custom/bin:/opt/custom/sbin; export PATH
PIDFILE=/var/run/statsd.pid
case "$1" in
'start')
#### STARTUP
rm -f -- $PIDFILE
cd /opt/local/statsd
/opt/local/bin/node stats.js config.js > /var/log/statd.log &
PID=$!
echo $PID > $PIDFILE
;;
'stop')
#### SHUTDOWN
kill `cat $PIDFILE`
rm -f -- $PIDFILE
;;
*)
echo "Usage: $0 { start | stop }"
exit $SMF_EXIT_ERR_FATAL
;;
esac
exit $SMF_EXIT_OK
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='export'>
<service name='graphite/statsd' type='service' version='0'>
<create_default_instance enabled='true'/>
<single_instance/>
<dependency name='net-physical' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/network/physical'/>
</dependency>
<dependency name='filesystem' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/system/filesystem/local'/>
</dependency>
<dependency name='carbon' grouping='require_all' restart_on='none' type='service'>
<service_fmri value='svc:/graphite/graphite'/>
</dependency>
<exec_method name='start' type='method' exec='/opt/custom/share/svc/statsd.sh %m' timeout_seconds='0'/>
<exec_method name='stop' type='method' exec='/opt/custom/share/svc/statsd.sh %m' timeout_seconds='60'/>
<property_group name='startd' type='framework'>
<propval name='duration' type='astring' value='transient'/>
</property_group>
<stability value='Unstable'/>
<template>
<common_name>
<loctext xml:lang='C'>StatsD Service</loctext>
</common_name>
</template>
</service>
</service_bundle>
var StatsdClient = new require('statsd-client');
console.dir(StatsdClient)
var sdc = new StatsdClient({host: '<IP?', port:8125, prefix:"test"});
console.dir(sdc)
var timer = new Date();
for (var i = 0; i < 100; i++) {
sdc.increment('counter'); // Increment by one.
};
sdc.increment('counter'); // Increment by one.
sdc.gauge('gauge', 10); // Set gauge to 10
sdc.timing('timer', timer); // Calculates time diff
sdc.close(); // Optional - stop NOW
forward port 8080 and port 8125 to that machines ip if necessary
create a folder for statsd
npm install statsd-client
{
"alias":"graphite",
"brand": "joyent",
"image_uuid": "84cb7edc-3f22-11e2-8a2a-3f2a7b148699",
"max_physical_memory": 512,
"resolvers": [
"8.8.8.8",
"8.8.4.4"
],
"nics": [
{
"interface": "net0",
"nic_tag": "admin",
"gateway": "192.168.2.254",
"ip": "192.168.2.203",
"netmask": "255.255.255.0"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment