Skip to content
jcelerier edited this page Sep 7, 2014 · 10 revisions

Welcome to the napd wiki!

napd will put your computer to sleep according to a set of easily defined rules (e.g. "vlc is not running" or "there are no active connections on mysql").

4 kind of things can be tracked :

  • PIDs (which must be specified at runtime).
  • running systemd units.
  • processes (by full path, basename, or cmdline).
  • custom scripts whose return value are checked.

Requirements

Something providing the logind D-Bus API, QtDBus, QMake. Also, if units file are to be checked, the systemd1 D-Bus API is required.

Building

cd your_build_folder
qmake path/to/src/Base.pro
make
make install

Configuration

The configuration format is similar to systemd unit files and is, for now, very likely to change. There are exemples files in the source tree (lib/napd/). napd checks for its configuration in /etc/napd... For now there is no install script (I guess it is more a "distro" work).

After creating the configuration files, they should be enabled with napctl (see below).

To check if a process is not running :

Location :

/etc/napd/processes.d

Format :

[Process]
# Any combination of MatchingPath, MatchingBasename, MatchingCmdline. 
MatchingPath=/opt/google/chrome-unstable/chrome
MatchingBasename=chrome
MatchingCmdline=/opt/google/chrome-unstable/chrome --type=gpu-broker
Timeout=42    # The number of second the timer waits if this process is found running.

To check if a unit is not running :

Location :

/etc/napd/units.d

Format :

[Unit]
UnitName=apache2.service # Mandatory
Timeout=42    # Facultative, the number of second the timer waits if this unit is found running.

To run a custom check

Location :

/etc/napd/checks.d

Format :

[Check]
Exec=/some/script.sh # Mandatory
MustEqual=46  # Expected return value of the program. Default is 0.
AsUser=napd   # Default is the user napd is ran as.
Timeout=42    # The number of second the timer waits if this check is true.

Control

napd comes with an utility named napctl.

  • Syntax :

    napctl command arguments

  • Available commands

    • add-pid PID [timeout] : adds a PID to track, with an optional timeout
    • remove-pid PID : removes a tracked PID
    • enable : enables a file in /etc/napd/{checks,processes,units}.d by making a symlink towards it in the active subfolder of the relevant folder
    • disable : disables a file in /etc/napd/{checks,processes,units}.d by removing it.

Example of scripts to use with the custom check

  • SSH: Returns 0 if there are no active SSH clients connected to this computer.

    [[ -z "$(netstat -t -n -A inet|awk -F ' ' '{print $4}'|grep ':22')" ]]

  • NFS: Returns 0 if no computers are mounting any NFS shares from this computer.

    [[ -z $(showmount|tail -n +2) ]]

  • SMB : Returns 0 if there are no Samba clients connected.

    [[ -z $(smbstatus -bp|grep -P '\b(?:\d{1,3}\.){3}\d{1,3}\b') ]]

  • MySQL : Returns 0 if there is nobody accessing the DB right now.

    [[ $(sudo mysqladmin -uYOURUSER -pYOURPASSWORD extended-status | grep -wi 'threads_connected' | awk '{ print $4}') -gt "1" ]]

  • Deluge : Returns 0 if there are no active torrents.

    [[ -z $(deluge-console info|egrep 'Downloading|Seeding') ]]