From 63539b2844fc62fa004854807d205be9847fe638 Mon Sep 17 00:00:00 2001
From: Jaka Hudoklin <jakahudoklin@gmail.com>
Date: Sun, 13 Oct 2013 23:10:29 +0200
Subject: [PATCH] nixos/graphite: carbon, use syslog for logging

Twisted provides option to log with syslog, this enables nicer logging.
Imagine what happens in a case of exception. If logs are written to stdout,
traceback won't be merged thus giving ugly logs. This commit fixes that.
This is also one of the official ways of starting carbon, so no worries.
---
 .../modules/services/monitoring/graphite.nix  | 50 ++++++++-----------
 1 file changed, 20 insertions(+), 30 deletions(-)

diff --git a/nixos/modules/services/monitoring/graphite.nix b/nixos/modules/services/monitoring/graphite.nix
index ec36db7b21c3..7326f1e73a03 100644
--- a/nixos/modules/services/monitoring/graphite.nix
+++ b/nixos/modules/services/monitoring/graphite.nix
@@ -5,7 +5,18 @@ with pkgs.lib;
 let
   cfg = config.services.graphite;
   writeTextOrNull = f: t: if t == null then null else pkgs.writeText f t;
+
   dataDir = "/var/db/graphite";
+  carbonOpts = name: with config.ids; ''
+    --nodaemon --syslog --prefix=${name} \
+    --uid ${toString uids.graphite} --gid ${toString uids.graphite} ${name}
+  '';
+  carbonEnv = {
+    PYTHONPATH = "${pkgs.python27Packages.carbon}/lib/python2.7/site-packages";
+    GRAPHITE_ROOT = dataDir;
+    GRAPHITE_CONF_DIR = "/etc/graphite/";
+  };
+
 in {
 
   ###### interface
@@ -52,7 +63,7 @@ in {
           [all_min]
           pattern = \.min$
           xFilesFactor = 0.1
-         aggregationMethod = min
+          aggregationMethod = min
         '';
       };
 
@@ -153,15 +164,8 @@ in {
       description = "Graphite data storage backend";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
-      environment = {
-        GRAPHITE_CONF_DIR = "/etc/graphite/";
-        GRAPHITE_STORAGE_DIR = "/var/db/graphite/";
-      };
-      serviceConfig = {
-        ExecStart = "${pkgs.pythonPackages.carbon}/bin/carbon-cache.py --pidfile /tmp/carbonCache.pid start";
-        User = "graphite";
-        Group = "graphite";
-      };
+      environment = carbonEnv;
+      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-cache"}";
       restartTriggers = [
         pkgs.pythonPackages.carbon
         cfg.carbon.config
@@ -178,15 +182,8 @@ in {
       description = "Carbon data aggregator";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
-      environment = {
-        GRAPHITE_CONF_DIR = "/etc/graphite/";
-        GRAPHITE_STORAGE_DIR = "${dataDir}";
-      };
-      serviceConfig = {
-        ExecStart = "${pkgs.pythonPackages.carbon}/bin/carbon-aggregator.py --pidfile /tmp/carbonAggregator.pid start";
-        User = "graphite";
-        Group = "graphite";
-      };
+      environment = carbonEnv;
+      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-aggregator"}";
       restartTriggers = [
         pkgs.pythonPackages.carbon cfg.carbon.config cfg.carbon.aggregationRules
       ];
@@ -196,15 +193,8 @@ in {
       description = "Carbon data relay";
       wantedBy = [ "multi-user.target" ];
       after = [ "network-interfaces.target" ];
-      environment = {
-        GRAPHITE_CONF_DIR = "/etc/graphite/";
-        GRAPHITE_STORAGE_DIR = "${dataDir}";
-      };
-      serviceConfig = {
-        ExecStart = "${pkgs.pythonPackages.carbon}/bin/carbon-relay.py --pidfile /tmp/carbonRelay.pid start";
-        User = "graphite";
-        Group = "graphite";
-      };
+      environment = carbonEnv;
+      serviceConfig.ExecStart = "${pkgs.twisted}/bin/twistd ${carbonOpts "carbon-relay"}";
       restartTriggers = [
         pkgs.pythonPackages.carbon cfg.carbon.config cfg.carbon.relayRules
       ];
@@ -218,7 +208,7 @@ in {
         PYTHONPATH = "${pkgs.python27Packages.graphite_web}/lib/python2.7/site-packages";
         DJANGO_SETTINGS_MODULE = "graphite.settings";
         GRAPHITE_CONF_DIR = "/etc/graphite/";
-        GRAPHITE_STORAGE_DIR = "${dataDir}";
+        GRAPHITE_STORAGE_DIR = dataDir;
       };
       serviceConfig = {
         ExecStart = ''
@@ -257,7 +247,7 @@ in {
       name = "graphite";
       uid = config.ids.uids.graphite;
       description = "Graphite daemon user";
-      home = "${dataDir}";
+      home = dataDir;
       createHome = true;
     };
     users.extraGroups.graphite.gid = config.ids.gids.graphite;