This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.sh
executable file
·138 lines (125 loc) · 3.61 KB
/
process.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# Archivo para procesar masivamente archivos MIBS via snmpttconvertmib
# Author: Rafael Amador Galvan
# Year: 2013
MIB_SOURCE="source/"
MIB_PROCESSED="processed/"
CONF_PATCH="patch/"
MIB_REPO="/usr/share/snmp/mibs/"
ZABBIX_TMP_CONF="zabbix/"
SNMP_CONF_DIR="/etc/snmp/"
SNMPTRAPD_CONF_DIR=$SNMP_CONF_DIR"snmptt.conf.d/"
#Internal variables
CONF_TEMP_DIR=""
MIB_NEW_FILE=""
FILENAME=""
# Enabling this flag disables all write operations over production directories
declare -i IS_DEBUG=0
declare -i FILES_COPIED=0
# Make sure only root can run our script
if (( $EUID != 0 ))
then
echo " Its Recommended executing this script as Root or sudo " 1>&2
# exit 1
fi
echo " ------------------------------------- "
echo " MIB Automatic processor. "
echo " Author: Rafael Amador Galvan - 2013 "
echo " ------------------------------------- "
echo " Source directory: $MIB_SOURCE "
# Initilizae extended patterns
shopt -s extglob
# Copying MIB files to the base diectory in the server
if (( $EUID == 0 || $IS_DEBUG == 1 ))
then
find $MIB_SOURCE -type f -print0 | while read -d '' -r file
do
MIB_NEW_FILE="$MIB_REPO${file##*/}"
MIB_NEW_FILE="${MIB_NEW_FILE%\.*}.txt"
if [ ! -e "$MIB_NEW_FILE" ]
then
cp -f $file $MIB_NEW_FILE
FILES_COPIED=$(($FILES_COPIED + 1))
fi
done
echo " $FILES_COPIED File(s) has been copied to $MIB_REPO."
else
echo " Not enough privileges to copy files to $MIB_REPO."
fi
find $MIB_SOURCE -print0 | while read -d '' -r file
do
if [ -d $file ]
then
CONF_TEMP_DIR="$MIB_PROCESSED${file#$MIB_SOURCE}"
if [ -d $CONF_TEMP_DIR ]; then
if [ -L $CONF_TEMP_DIR ]; then
# It is a symlink!
:
else
# It's a directory!
:
fi
else
mkdir $CONF_TEMP_DIR
fi
# echo "directorio $CONF_TEMP_DIR"
else
MIB_NEW_FILE="${file%\.*}.conf"
CONF_TEMP_DIR="$MIB_PROCESSED${MIB_NEW_FILE#$MIB_SOURCE}"
# echo $MIB_NEW_FILE
# echo $CONF_TEMP_DIR
if [ -e $CONF_TEMP_DIR ]
then
rm -f $CONF_TEMP_DIR
fi
snmpttconvertmib --in="$file" --out="$CONF_TEMP_DIR" 1>./convertmibs.txt 2>./error.log
FILE_SIZE=$( stat -c %s $CONF_TEMP_DIR )
if (( FILE_SIZE < 200 ))
then
echo "[SKIP] $CONF_TEMP_DIR"
rm -f $CONF_TEMP_DIR
else
echo "[OK] $CONF_TEMP_DIR"
fi
fi
done
if (( $EUID == 0 || $IS_DEBUG == 1 ))
then
echo "[INFO] Processing SNMP Traps to be used in Zabbix"
if [ ! -d $ZABBIX_CONF_DIR ]
then
mkdir $ZABBIX_CONF_DIR
fi
find $MIB_PROCESSED'/' -type f -print0 -exec cp {} $ZABBIX_TMP_CONF \;
echo ""
echo "--------------------------------------------------------"
echo "[INFO] Applying patches to config files (snmptt)"
find $CONF_PATCH -type f -print0 | while read -d '' -r file
do
FILENAME=$(basename "$file")
FILENAME="${FILENAME%.*}"
# echo "$ZABBIX_TMP_CONF$FILENAME"
if [ -e "$ZABBIX_TMP_CONF$FILENAME" ]
then
if (( $IS_DEBUG == 1 ))
then
patch --dry-run "$ZABBIX_TMP_CONF$FILENAME" <"$file"
else
patch "$ZABBIX_TMP_CONF$FILENAME" <"$file"
fi
fi
done
find $ZABBIX_TMP_CONF -type f -print0 -exec sed -i 's,FORMAT,FORMAT ZBXTRAP \$aA,g' {} +
cp -R -u -p $ZABBIX_TMP_CONF'.' $SNMPTRAPD_CONF_DIR
echo ""
echo "[INFO] Recopiling Info about the previously processed Files."
ls $SNMPTRAPD_CONF_DIR >./zfiles.txt
sed -i -e "s#^#$SNMPTRAPD_CONF_DIR#g" ./zfiles.txt
echo "[INFO] Reconfiguring SNMPTT "
sed -i -e '/snmptt_conf_files = <<END/,/END/{ # for a here-doc block:
/^END$/b # if at the end skip rest
/<<END/!d # if not first line delete and skip rest... else insert file:
r ./zfiles.txt
}' $SNMP_CONF_DIR./snmptt.ini
echo "[INFO] Reconfiguring Zabbix "
fi