-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add kswapd0hack service to defend against kswapd0 consuming all of the CPU.
- Loading branch information
Showing
6 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
################################################################################ | ||
# | ||
# Copyright (C) 2019 Neighborhood Guard, Inc. All rights reserved. | ||
# Original author: Douglas Kerr | ||
# | ||
# This file is part of CommunityView. | ||
# | ||
# CommunityView is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# CommunityView is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with FTP_Upload. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
################################################################################ | ||
|
||
[Unit] | ||
Description=Monitor kswapd0 CPU and reboot if too high | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
User=root | ||
WorkingDirectory=/var/opt/communityview/log | ||
ExecStart=/bin/sh /opt/communityview/kswapd0hack | ||
Restart=always | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/bin/sh | ||
################################################################################ | ||
# | ||
# Copyright (C) 2019 Neighborhood Guard, Inc. All rights reserved. | ||
# Original author: Douglas Kerr | ||
# | ||
# This file is part of CommunityView. | ||
# | ||
# CommunityView is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# CommunityView is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with FTP_Upload. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
################################################################################ | ||
|
||
# If kswapd0 uses over 10% of the CPU, reboot. | ||
# Check every ten minutes. | ||
|
||
logfile=/var/opt/communityview/log/kswapd0hack.log | ||
|
||
ckcpu() { | ||
kspct=`ps -C kswapd0 -o '%cpu' --no-header` | ||
|
||
# get integer portion of CPU percentage | ||
kspct_i=`echo "$kspct" | sed 's/ *\([0-9]*\).*/\1/'` | ||
|
||
if expr $kspct_i '>=' 1 > /dev/null | ||
then | ||
echo `date --iso-8601=seconds` \ | ||
"kswapd0 using" $kspct"% of CPU." \ | ||
>> "$logfile" | ||
echo `date --iso-8601=seconds` `uptime` \ | ||
>> "$logfile" | ||
fi | ||
if expr $kspct_i '>=' 10 > /dev/null | ||
then | ||
echo `date --iso-8601=seconds` \ | ||
"kswapd0 using" $kspct"% of CPU. Rebooting." \ | ||
>> "$logfile" | ||
echo `date --iso-8601=seconds` `uptime` \ | ||
>> "$logfile" | ||
ps auxww >> "$logfile" | ||
mv "$logfile" "$logfile.old" | ||
shutdown -r now | ||
fi | ||
} | ||
|
||
if [ ! "$UNIT_TEST_IN_PROGRESS" ] | ||
then | ||
while true | ||
do | ||
ckcpu | ||
sleep 600 | ||
done | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/bin/sh | ||
################################################################################ | ||
# | ||
# Copyright (C) 2020 Neighborhood Guard, Inc. All rights reserved. | ||
# Original author: Douglas Kerr | ||
# | ||
# This file is part of FTP_Upload. | ||
# | ||
# FTP_Upload is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# FTP_Upload is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with FTP_Upload. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
################################################################################ | ||
|
||
UNIT_TEST_IN_PROGRESS=1 | ||
|
||
. ../kswapd0hack.sh | ||
|
||
percentage='' | ||
shutdowncalled='' | ||
|
||
logfile=test.log | ||
|
||
setUp() { | ||
rm -rf test.log test.log.old | ||
shutdowncalled='' | ||
} | ||
|
||
shutdown() { | ||
shutdowncalled=1 | ||
} | ||
|
||
ps() { | ||
echo "$percentage" | ||
} | ||
|
||
test_lt_1pct() { | ||
percentage=" 0.9" | ||
ckcpu | ||
# log file should not exist; no call to shutdown | ||
assertFalse 'Log file created erroneously' "test -r $logfile" | ||
assertFalse 'shutdown called erroneously' "test -n \"$shutdowncalled\"" | ||
} | ||
|
||
test_eq_1pct() { | ||
percentage=" 1.0" | ||
ckcpu | ||
# log file should exist; no call to shutdown | ||
assertTrue 'Log file not created' "test -r $logfile" | ||
assertFalse 'shutdown called erroneously' "test -n \"$shutdowncalled\"" | ||
} | ||
|
||
test_gt_1pct() { | ||
percentage=" 2.0" | ||
ckcpu | ||
# log file should exist; no call to shutdown | ||
assertTrue 'Log file not created' "test -r $logfile" | ||
assertFalse 'shutdown called erroneously' "test -n \"$shutdowncalled\"" | ||
} | ||
|
||
test_eq_10pct() { | ||
percentage=" 10.0" | ||
ckcpu | ||
# log file should exist; no call to shutdown | ||
assertTrue 'Log file not rotated' "test -r $logfile.old" | ||
assertTrue 'shutdown not called' "test -n \"$shutdowncalled\"" | ||
} | ||
|
||
test_gt_10pct() { | ||
percentage=" 99.0" | ||
ckcpu | ||
# log file should exist; no call to shutdown | ||
assertTrue 'Log file not rotated' "test -r $logfile.old" | ||
assertTrue 'shutdown not called' "test -n \"$shutdowncalled\"" | ||
} | ||
|
||
. `which shunit2` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters