-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
130 lines (103 loc) · 3.62 KB
/
install.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
#!/bin/bash
####
# @since: 2021-08-30
# @author: stev leibelt <[email protected]>
####
#end of variables declaration
CONFIGURATION_FILE_EXPECTED_VERSION=1
CONFIGURATION_FILE_PATH="/etc/net.bazzline/zabbix/housekeeping/local_configuration.sh"
WHO_AM_I=$(whoami)
#end of variables declaration
#begin of check if we are root
if [[ ${WHO_AM_I} != "root" ]];
then
#call this script (${0}) again with sudo with all provided arguments (${@})
sudo "${0}" "${@}"
exit ${?}
fi
#end of check if we are root
#begin of install routine
echo ":: Creating files."
mkdir -p /etc/net.bazzline/zabbix/housekeeping
# begin of configuration file
echo ":: Please input your zabbix database user name."
read DB_USERNAME
echo ":: Please input your zabbix database user password."
read DB_PASSWORD
cat > /etc/net.bazzline/zabbix/housekeeping/local_configuration.sh<<DELIM
#!/bin/bash
####
# @author: https://github.com/bazzline/zabbix_mysql_housekeeping/blob/main/bin/install.sh
####
CURRENT_VERSION=1
DB_USERNAME='${DB_USERNAME}'
DB_PASSWORD='<${DB_PASSWORD}>'
DELIM
echo ":: Please check the configuration file."
echo " Path >>${CONFIGURATION_FILE_PATH}<<."
# end of configuration file
# begin of executable file
cat > /etc/net.bazzline/zabbix/housekeeping/housekeeping.sh<<DELIM
#!/bin/bash
####
# @since: 2021-08-30
# @author: stev leibelt <[email protected]>
####
logger -i -p cron.debug "bo: maintenance."
CONFIGURATION_FILE_PATH='/etc/net.bazzline/zabbix/housekeeping/local_configuration.sh'
CONFIGURATION_FILE_EXPECTED_VERSION=1
if [[ -f \${CONFIGURATION_FILE_PATH} ]];
then
source \${CONFIGURATION_FILE_PATH}
if [[ \${CURRENT_VERSION} -ne \${CONFIGURATION_FILE_EXPECTED_VERSION} ]];
then
logger -i -p cron.crit ":: Configuration version is wrong."
logger -i -p cron.crit " Expected >>\${CONFIGURATION_FILE_EXPECTED_VERSION}<<, found >>\${CURRENT_VERSION}<<."
exit 2
fi
else
logger -i -p cron.crit ":: Expected configuration file not found."
logger -i -p cron.crit " >>\${CONFIGURATION_FILE_PATH}<< is not a file."
exit 1
fi
logger -i -p cron.notice " Starting >>optimize<< for table >>zabbix.history_uint<<."
mysqlcheck -u\${DB_USERNAME} -p\${DB_PASSWORD} --optimize zabbix history_uint;
logger -i -p cron.notice " Starting >>optimize<< for table >>zabbix.history<<."
mysqlcheck -u\${DB_USERNAME} -p\${DB_PASSWORD} --optimize zabbix history;
logger -i -p cron.debug "eo: maintenance."
DELIM
chmod +x /etc/net.bazzline/zabbix/housekeeping/housekeeping.sh
# end of executable file
# begin of service file
cat > /etc/net.bazzline/zabbix/housekeeping/zabbix-housekeeping.service<<DELIM
[Unit]
Description=net.bazzline zabbix housekeeping service
ConditionACPower=true
After=zabbix-server-mysql.service
[Service]
Type=oneshot
ExecStart=/etc/net.bazzline/zabbix/housekeeping/housekeeping.sh
KillMode=process
TimeoutStopSec=21600
DELIM
# end of service file
# end of timer file
# begin of timer file
cat > /etc/net.bazzline/zabbix/housekeeping/weekly-zabbix-housekeeping.timer<<DELIM
[Unit]
Description=Weekly zabbix mysql housekeeping
[Timer]
OnCalendar=Sat *-*-* 05:00
RandomizedDelaySec=42
Persistent=true
Unit=zabbix-housekeeping.service
[Install]
WantedBy=timers.target
DELIM
# end of timer file
echo ":: Adding and enabling systemd files."
cp /etc/net.bazzline/zabbix/housekeeping/zabbix-housekeeping.service /etc/systemd/system/zabbix-housekeeping.service
cp /etc/net.bazzline/zabbix/housekeeping/weekly-zabbix-housekeeping.timer /etc/systemd/system/weekly-zabbix-housekeeping.timer
systemctl daemon-reload
systemctl enable weekly-zabbix-housekeeping.timer
#end of install routine