Skip to content

Commit

Permalink
Merge pull request #189 from wrboyce/pkg-telegraf
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgarga committed May 9, 2017
2 parents adda461 + 6845460 commit 270e09e
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 0 deletions.
41 changes: 41 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# $FreeBSD$

PORTNAME= pfSense-pkg-Telegraf
PORTVERSION= 0.1
CATEGORIES= net-mgmt
MASTER_SITES= # empty
DISTFILES= # empty
EXTRACT_ONLY= # empty

MAINTAINER= me@willboyce.com
COMMENT= pfSense package Telegraf

LICENSE= APACHE20

RUN_DEPENDS= telegraf:net-mgmt/telegraf

NO_BUILD= yes
NO_MTREE= yes

SUB_FILES= pkg-install pkg-deinstall
SUB_LIST= PORTNAME=${PORTNAME}

do-extract:
${MKDIR} ${WRKSRC}

do-install:
${MKDIR} ${STAGEDIR}${PREFIX}/pkg
${MKDIR} ${STAGEDIR}/etc/inc/priv
${MKDIR} ${STAGEDIR}${DATADIR}
${INSTALL_DATA} -m 0644 ${FILESDIR}${PREFIX}/pkg/telegraf.xml \
${STAGEDIR}${PREFIX}/pkg
${INSTALL_DATA} ${FILESDIR}${PREFIX}/pkg/telegraf.inc \
${STAGEDIR}${PREFIX}/pkg
${INSTALL_DATA} ${FILESDIR}/etc/inc/priv/telegraf.priv.inc \
${STAGEDIR}/etc/inc/priv
${INSTALL_DATA} ${FILESDIR}${DATADIR}/info.xml \
${STAGEDIR}${DATADIR}
@${REINPLACE_CMD} -i '' -e "s|%%PKGVERSION%%|${PKGVERSION}|" \
${STAGEDIR}${DATADIR}/info.xml

.include <bsd.port.mk>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
global $priv_list;

$priv_list['page-services-telegraf'] = array();
$priv_list['page-services-telegraf']['name'] = "WebCfg - Services: Telegraf";
$priv_list['page-services-telegraf']['descr'] = "Allow access to Telegraf package GUI";
$priv_list['page-services-telegraf']['match'] = array();
$priv_list['page-services-telegraf']['match'][] = "pkg_edit.php?xml=telegraf.xml*";
?>
3 changes: 3 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/files/pkg-deinstall.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/usr/local/bin/php -f /etc/rc.packages %%PORTNAME%% ${2}
7 changes: 7 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/files/pkg-install.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

if [ "${2}" != "POST-INSTALL" ]; then
exit 0
fi

/usr/local/bin/php -f /etc/rc.packages %%PORTNAME%% ${2}
120 changes: 120 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/files/usr/local/pkg/telegraf.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/*
* telegraf.inc
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2017 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require_once("functions.inc");
require_once("globals.inc");
require_once("pkg-utils.inc");
require_once("service-utils.inc");
require_once("util.inc");

function telegraf_resync_config() {
global $g, $config;

$telegraf_conf = array();
if (is_array($config['installedpackages']['telegraf'])) {
foreach ($config['installedpackages']['telegraf']['config'][0] as $k => $v) {
$telegraf_conf[$k] = addslashes($v);
}
}

conf_mount_rw();

/* disable telegraf if not enabled */
if ($telegraf_conf['enable'] != "on") {
if (is_service_running("telegraf")) {
stop_service("telegraf");
}
unlink_if_exists("/usr/local/etc/rc.d/telegraf.sh");
unlink_if_exists("/usr/local/etc/telegraf.conf");
return;
}

if (empty($telegraf_conf['interval'])) {
$telegraf_conf['interval'] = 10;
}

/* generate telegraf.conf */
$cfg = <<< EOD
# This file is automatically generated by pfSense #
[agent]
interval = "{$telegraf_conf['interval']}s"
round_interval = true
[[inputs.cpu]]
percpu = true
totalcpu = true
fielddrop = ["time_*"]
[[inputs.disk]]
ignore_fs = ["tmpfs", "devtmpfs"]
[[inputs.diskio]]
[[inputs.kernel]]
[[inputs.mem]]
[[inputs.net]]
[[inputs.processes]]
[[inputs.swap]]
[[inputs.system]]
[[outputs.influxdb]]
[[outputs.influxdb]]
urls = ["{$telegraf_conf['influx_server']}"]
database = "{$telegraf_conf['influx_db']}"
EOD;

if (!empty($telegraf_conf['influx_user'])) {
$cfg .= "\tusername = \"" . $telegraf_conf['influx_user'] . "\"\n";
}
if (!empty($telegraf_conf['influx_pass'])) {
$cfg .= "\tpassword = \"" . $telegraf_conf['influx_pass'] . "\"\n";
}
$conffile = "/usr/local/etc/telegraf.conf";
file_put_contents($conffile, $cfg);

/* generate telegraf.sh rcfile */
$pidfile = "{$g['varrun_path']}/telegraf.pid";
$logfile = "{$g['varlog_path']}/telegraf.log";
write_rcfile(array(
"file" => "telegraf.sh",
"start" => "/usr/sbin/daemon -crP {$pidfile} /usr/local/bin/telegraf -config={$conffile} 2> {$logfile}",
"stop" => "/bin/kill `/bin/cat {$pidfile}`"
)
);

/* (re)start service */
if (is_service_running("telegraf")) {
restart_service("telegraf");
} else {
start_service("telegraf");
}

sleep(1);
conf_mount_ro();
}
?>
84 changes: 84 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/files/usr/local/pkg/telegraf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE packagegui SYSTEM "../schema/packages.dtd">
<?xml-stylesheet type="text/xsl" href="../xsl/package.xsl"?>
<packagegui>
<copyright>
<![CDATA[
/*
* telegraf.xml
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2017 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
]]>
</copyright>
<name>telegraf</name>
<title>Services: Telegraf</title>
<include_file>/usr/local/pkg/telegraf.inc</include_file>
<menu>
<name>Telegraf</name>
<section>Services</section>
<configfile>telegraf.xml</configfile>
<url>/pkg_edit.php?xml=telegraf.xml</url>
</menu>
<service>
<name>telegraf</name>
<rcfile>telegraf.sh</rcfile>
<executable>telegraf</executable>
<description>Telegraf daemon</description>
</service>
<fields>
<field>
<fielddescr>Enable</fielddescr>
<fieldname>enable</fieldname>
<type>checkbox</type>
<description>Check to enable Telegraf.</description>
<enablefields>interval,influx_server,influx_db,influx_user,influx_pass</enablefields>
</field>
<field>
<fielddescr>Update Interval</fielddescr>
<fieldname>interval</fieldname>
<type>input</type>
<description>Update interval (seconds) (default: 10).</description>
</field>
<field>
<fielddescr>InfluxDB Server</fielddescr>
<fieldname>influx_server</fieldname>
<type>input</type>
<description>Full HTTP or UDP endpoint URL for InfluxDB instance</description>
</field>
<field>
<fielddescr>InfluxDB Database</fielddescr>
<fieldname>influx_db</fieldname>
<type>input</type>
<description>Target database for metrics (created if does not exist)</description>
</field>
<field>
<fielddescr>InfluxDB Username</fielddescr>
<fieldname>influx_user</fieldname>
<type>input</type>
</field>
<field>
<fielddescr>InfluxDB Password</fielddescr>
<fieldname>influx_pass</fieldname>
<type>password</type>
<encoding>base64</encoding>
</field>
</fields>
<custom_php_resync_config_command>
telegraf_resync_config();
</custom_php_resync_config_command>
</packagegui>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<pfsensepkgs>
<package>
<name>Telegraf</name>
<descr><![CDATA[Telegraf is an agent written in Go for collecting, processing, aggregating, and writing metrics.]]></descr>
<version>%%PKGVERSION%%</version>
<configurationfile>telegraf.xml</configurationfile>
</package>
</pfsensepkgs>
2 changes: 2 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/pkg-descr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Telegraf is an agent written in Go for collecting, processing, aggregating, and
writing metrics.
6 changes: 6 additions & 0 deletions net-mgmt/pfSense-pkg-Telegraf/pkg-plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pkg/telegraf.xml
pkg/telegraf.inc
/etc/inc/priv/telegraf.priv.inc
%%DATADIR%%/info.xml
@dir /etc/inc
@dir /etc/inc/priv

0 comments on commit 270e09e

Please sign in to comment.