-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·130 lines (114 loc) · 2.89 KB
/
setup.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
nparse=/usr/lib/nethogs-parser
nlog=/opt/netlog
logdir=/var/log/netlog
bpath=/usr/bin
cnf=/etc/logrotate.d/netlog
# Install dependencies
apt-get update
apt-get install nethogs golang-go -y
# Create files and directories
if [ ! -d $nlog ]; then
echo "Setting up netlog..."
mkdir -p $nparse $nlog $logdir
cp nethogs-parser/hogs.go $nparse
cat > $nlog/netlog << EOF
#!/bin/bash
nethogs -t | tee -a /var/log/netlog/daily /var/log/netlog/monthly
EOF
cat > $bpath/netlog-parser << EOF
#!/bin/bash
echo -e "\\n"
printf " _ _ _ _ \\n| \\ | | | | | | \\n| \\| | ___| |_| | ___ __ _ \\n| . ' |/ _ \\ __| |/ _ \\ / _' |\\n| |\\ | __/ |_| | (_) | (_| |\\n\\_| \\_/\\___|\\__|_|\\___/ \\__, |\\n __/ |\\n |___/ \\n\\n"
echo "Data is available for:"
printf "\n"
files=\$(ls -I "*.gz" /var/log/netlog/)
while true; do
i=1
for j in \$files
do
echo "\$i. \$j"
file[i]=\$j
i=\$(( i + 1 ))
done
printf "\n"
echo "Enter number or 0 to exit:"
read input
number=\$(printf '%s\\n' "\$input" | tr -dc '[:digit:]')
if [ "\$number" == "0" ]; then
printf "\\n"
exit
elif [ -z \$number ]; then
printf "\\n"
echo "No value selected"
printf "\\n"
elif [ "\$number" -gt "\$i" ]; then
printf "\\n"
echo "Invalid selection"
printf "\\n"
else
printf "\\n"
go run /usr/lib/nethogs-parser/hogs.go -type=pretty /var/log/netlog/\${file[\$input]}
break
fi
done
EOF
chmod +x $nlog/netlog
chmod +x $bpath/netlog-parser
chmod 775 $logdir
fi
echo "Setting up netlog service..."
IS_ACTIVE=$(sudo systemctl is-active netlog)
if [ "$IS_ACTIVE" == "active" ]; then
# Restart netlog service if active
echo "Service is running"
echo "Restarting service..."
systemctl restart netlog
echo "Service has restarted"
else
# Create the netlog service file
echo "Creating service file"
cat > /etc/systemd/system/netlog.service << EOF
[Unit]
Description=Netlog Nethogs daemon
After=network.target
[Service]
Type=simple
Restart=on-failure
User=root
ExecStart=/opt/netlog/netlog
[Install]
WantedBy=multi-user.target
EOF
# Enable and start the netlog service
echo "Enabling the netlog service..."
systemctl daemon-reload
systemctl enable netlog
systemctl start netlog
echo "Netlog service started!"
fi
# Setup log rotation
if [ ! -f $cnf ]; then
cat > $cnf << EOF
/var/log/netlog/daily {
daily
rotate 30
dateext
create
postrotate
systemctl restart netlog
endscript
}
/var/log/netlog/monthly {
monthly
rotate 3
compress
dateext
create
postrotate
systemctl restart netlog
endscript
}
EOF
fi
exit 0