4f91a453feeefc16a1fb2b7a740b187feeac3096
[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 # Config file options are changing between releases so we have to store both
17 # configurations variants and choose correct one for target collectd version
18
19 if [ -z "$1" ]; then
20     echo "Error! Please sample configs variant name as a param!"\
21          "(name of directory with sample-configs)"
22     echo "Usage:"
23     echo "$0 SAMPLE_CONFIGS_VARIANT_NAME"
24     echo "e.g. $0 'collectd_sample_configs'"
25     exit 1
26 fi
27
28 SAMPLE_CONF_VARIANT="$1"
29 COLLECTD_CONF_FILE=/opt/collectd/etc/collectd.conf
30 COLLECTD_CONF_DIR=/opt/collectd/etc/collectd.conf.d
31 INCLUDE_CONF="<Include \"/opt/collectd/etc/collectd.conf.d\">"
32 CURR_DIR=`pwd`
33 HOSTNAME=`hostname`
34 SAMPLE_CONF_DIR=$CURR_DIR/$SAMPLE_CONF_VARIANT
35
36 if [ ! -d "$SAMPLE_CONF_DIR" ]; then
37     echo "$SAMPLE_CONF_DIR does not exits!"\
38          "Probably passed bad variant name as a param: $SAMPLE_CONF_VARIANT"
39     exit 1
40 fi
41
42 function write_include {
43     echo "Hostname \"$HOSTNAME\"" | sudo tee -a $COLLECTD_CONF_FILE;
44     echo $INCLUDE_CONF | sudo tee -a $COLLECTD_CONF_FILE;
45     echo "  Filter \"*.conf\"" | sudo tee -a $COLLECTD_CONF_FILE;
46     echo -e "</Include>" | sudo tee -a $COLLECTD_CONF_FILE;
47 }
48
49 grep -qe '<Include "/opt/collectd/etc/collectd.conf.d">' $COLLECTD_CONF_FILE; [ $? -ne 0 ] && write_include
50
51 `mkdir -p $COLLECTD_CONF_DIR`
52
53 SAMPLE_CONF_FILES=$SAMPLE_CONF_DIR/*
54 for F in $SAMPLE_CONF_FILES; do
55    FILE=$(basename $F)
56    [ -f $COLLECTD_CONF_DIR/$FILE ] && echo "File $COLLECTD_CONF_DIR/$FILE exists" || cp $F $COLLECTD_CONF_DIR
57 done