src: ensure hostname is included in collectd conf
[barometer.git] / src / collectd / include_config.sh
1 #!/bin/bash
2 # Copyright 2017 OPNFV
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 COLLECTD_CONF_FILE=/opt/collectd/etc/collectd.conf
17 COLLECTD_CONF_DIR=/opt/collectd/etc/collectd.conf.d
18 INCLUDE_CONF="<Include \"/opt/collectd/etc/collectd.conf.d\">"
19 CURR_DIR=`pwd`
20 HOSTNAME=`hostname`
21 SAMPLE_CONF_DIR=$CURR_DIR/collectd_sample_configs/*
22
23 function write_include {
24     echo "Hostname \"$HOSTNAME\"" | sudo tee -a $COLLECTD_CONF_FILE;
25     echo $INCLUDE_CONF | sudo tee -a $COLLECTD_CONF_FILE;
26     echo "  Filter \"*.conf\"" | sudo tee -a $COLLECTD_CONF_FILE;
27     echo -e "</Include>" | sudo tee -a $COLLECTD_CONF_FILE;
28 }
29
30 grep -qe '<Include "/opt/collectd/etc/collectd.conf.d">' $COLLECTD_CONF_FILE; [ $? -ne 0 ] && write_include
31
32 `mkdir -p $COLLECTD_CONF_DIR`
33
34 for F in $SAMPLE_CONF_DIR; do
35    FILE=$(basename $F)
36    [ -f $COLLECTD_CONF_DIR/$FILE ] && echo "File $COLLECTD_CONF_DIR/$FILE exists" || cp $F $COLLECTD_CONF_DIR
37 done