Skip to content

Commit

Permalink
Release 1.1.0
Browse files Browse the repository at this point in the history
- Add kswapd0hack service to defend against kswapd0 consuming all
of the CPU.
  • Loading branch information
dougkerr committed Mar 10, 2020
2 parents 7292cc2 + 94dea38 commit b92cdbc
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 6 deletions.
23 changes: 21 additions & 2 deletions CommunityView/confcvserver/confcvserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# CommunityView software.

# version of the confcvserver software
version="1.0.2"
version="1.1.0"

. ./utils.sh
#. ./confui.sh
Expand All @@ -52,6 +52,7 @@ config_dir=/etc/opt/communityview # XXX future use
var_dir=/var/opt/communityview
log_dir=$var_dir/log
systemd_dir=/lib/systemd/system
kswapd0hack_code_dir=/opt/communityview

# log file for this script
scriptlog=confcvserver.log
Expand Down Expand Up @@ -229,7 +230,7 @@ configure() {
# and remove the service file if it's there
systemctl stop communityview || true
systemctl disable communityview || true
tgt=$systemd_dir/communityview.service
local tgt=$systemd_dir/communityview.service
rm -f "$tgt"

task="creating the upload user account"
Expand Down Expand Up @@ -455,6 +456,24 @@ configure() {
chmod 755 $code_dir/$name
editcrontab $name "7 8,14,20 * * * $code_dir/$name"

task="installing hack to defend against kswapd0 bug"
echo "***** $task" | tee /dev/tty
local name=kswapd0hack
systemctl stop $name.service || true
systemctl disable $name.service || true
tgt=$systemd_dir/$name.service
rm -f "$tgt"
cp $our_dir/../$name/$name.service "$tgt"
chmod 644 "$tgt"
chown root:root "$tgt"
tgt="$kswapd0hack_code_dir"/$name
mk_dir "$kswapd0hack_code_dir"
cp $our_dir/../$name/$name.sh "$tgt"
chmod 755 "$tgt"
chown root:root "$tgt"
systemctl enable $name.service
systemctl start $name.service

# accounts-daemon seems to have a bug wherein it frequently goes crazy
# and sucks up all the CPU
task="permanently disabling accounts-daemon"
Expand Down
13 changes: 10 additions & 3 deletions CommunityView/doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Release Notes for CommunityView #

## v1.0.2 - 2019/06/25
## v1.1.0 - 2020/03/09
_Doug Kerr_

### Changes

- Fix crash in stats code when trying to remove temp file after non-graceful
shutdown
- Implement _kswapd0hack_ service. This service defends against the problem of the _kswapd0_ process eventually taking all of the CPU when the system is under heavy load. It reboots the machine when _kswapd0_ CPU consumption rises to 10%. Web search for "kswapd0 taking a lot of cpu" to see many write-ups on this problem.

### To Do

Expand All @@ -20,6 +19,14 @@ shutdown

* The `Next day` links in day pages are sometimes incorrectly grayed out.

## v1.0.2 - 2019/06/25
_Doug Kerr_

### Changes

- Fix crash in stats code when trying to remove temp file after non-graceful
shutdown

## v1.0.1 - 2019/02/12
_Doug Kerr_

Expand Down
35 changes: 35 additions & 0 deletions CommunityView/kswapd0hack/kswapd0hack.service
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
63 changes: 63 additions & 0 deletions CommunityView/kswapd0hack/kswapd0hack.sh
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
86 changes: 86 additions & 0 deletions CommunityView/kswapd0hack/test/testKswapd0hack.sh
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`
2 changes: 1 addition & 1 deletion CommunityView/src/communityview.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# #
################################################################################

version_string = "1.0.2"
version_string = "1.1.0"


import os
Expand Down

0 comments on commit b92cdbc

Please sign in to comment.