Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
groland11 committed Feb 1, 2023
2 parents e2e7426 + c0ed9ac commit 28f03b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Similar to "needs-restarting" in Red Hat Enterprise Linux, do-restarting actuall
- Supports blacklisting and whitelisting of services in configuration file
- Individual configuration for each service: Day of week and hours allowed for restart, pre- and post-command

## Requirements
- Red Hat Enterprise Linux 7/8/9
- Python >= 3.6
- Package "yum-utils" (needs-restarting)

## Usage
```
./do-restarting.py -h
Expand Down Expand Up @@ -68,8 +73,8 @@ optional arguments:
# Service names must not include the trailing ".service".
# Example: whitelist=dbus
[MAIN]
blacklist=httpd,mysqld
whitelist=dbus
blacklist=mysqld
whitelist=dbus,httpd,firewalld
### Sections for individual services
# Following configuration values are supported:
Expand Down
42 changes: 25 additions & 17 deletions do-restarting.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
"/opt/nessus_agent/sbin/": "nessusagent",
"/bin/bash /usr/bin/check_mk_agent": "check_mk-async",
"/usr/sbin/dhcpd": "dhcpd",
"/usr/local/bin/c-icap": "c-icap",
"/usr/sbin/squid": "squid",
"/usr/sbin/irqbalance": "irqbalance",
"/usr/libexec/udisks2/udisksd": "udisks2",
"/usr/libexec/platform-python /usr/bin/virt-who": "virt-who",
"/usr/bin/lsmd ": "libstoragemgmt",
"/sbin/agetty .* tty1 ": "getty\x40tty1"
Expand Down Expand Up @@ -423,24 +426,29 @@ def get_daemons() -> set:
raise Exception
else:
if output.returncode != 0:
logger.error(f"needs-restarting returned {output.returncode}: {output.stderr.strip()}")
raise Exception
else:
for line in output.stdout.splitlines():
try:
cmd = line.split(":")[1].strip()
except IndexError as e:
logger.debug(f"Skipping output line '{line}'")
# Ignore error output: "Failed to read PID ...", "[Errno 2] No such file or directory ..."
m1 = re.match(r'Failed to read PID', output.stderr.strip())
m2 = re.match(r'\[Errno 2\] No such file or directory', output.stderr.strip())
m3 = re.match(r'\[Errno 3\] No such process', output.stderr.strip())
if not m1 and not m2 and not m3:
logger.error(f"needs-restarting returned {output.returncode}: {output.stderr.strip()}")
raise Exception

for line in output.stdout.splitlines():
try:
cmd = line.split(":")[1].strip()
except IndexError as e:
logger.debug(f"Skipping output line '{line}'")
else:
for process in MAP:
m = re.match(f"{process}", cmd)
#if cmd.startswith(process):
if m:
daemon = MAP[process]
daemons.add(daemon) if daemon not in BLACKLIST and daemon != "" else logger.debug(f"Skipping {cmd} ({daemon if daemon != '' else '<no daemon process>'})")
break
else:
for process in MAP:
m = re.match(f"{process}", cmd)
#if cmd.startswith(process):
if m:
daemon = MAP[process]
daemons.add(daemon) if daemon not in BLACKLIST and daemon != "" else logger.debug(f"Skipping {cmd} ({daemon if daemon != '' else '<no daemon process>'})")
break
else:
logger.debug(f"Unknown process {cmd}")
logger.debug(f"Unknown process {cmd}")

return daemons

Expand Down

0 comments on commit 28f03b4

Please sign in to comment.