|
11 | 11 | """See the check's README for more details. |
12 | 12 | """ |
13 | 13 |
|
14 | | -import argparse # pylint: disable=C0413 |
15 | | -import sys # pylint: disable=C0413 |
| 14 | +import argparse |
| 15 | +import sys |
16 | 16 |
|
17 | | -import lib.args # pylint: disable=C0413 |
18 | | -import lib.base # pylint: disable=C0413 |
19 | | -import lib.lftest # pylint: disable=C0413 |
20 | | -import lib.shell # pylint: disable=C0413 |
| 17 | +import lib.args |
| 18 | +import lib.base |
| 19 | +import lib.lftest |
| 20 | +import lib.shell |
21 | 21 | from lib.globals import (STATE_OK, STATE_UNKNOWN) |
22 | 22 |
|
23 | 23 | __author__ = 'Linuxfabrik GmbH, Zurich/Switzerland' |
24 | | -__version__ = '2025100601' |
| 24 | +__version__ = '2026040801' |
25 | 25 |
|
26 | 26 | DESCRIPTION = """Counts how many users are currently logged in, both via tty (on Windows: Console) |
27 | 27 | and pts (on Linux: typically ssh, on Windows: RDP). Also counts the disconnected |
@@ -81,22 +81,37 @@ def parse_args(): |
81 | 81 |
|
82 | 82 | def parse_linux_output(s): |
83 | 83 | """Parse the output of `w` on Linux. |
| 84 | +
|
| 85 | + Uses the header line to determine the TTY column position, so it works |
| 86 | + regardless of whether FROM is present, and regardless of column widths |
| 87 | + (which vary across distros and versions). |
84 | 88 | """ |
85 | 89 | # replace pipes in output, otherwise we will get problems with perfdata, |
86 | 90 | # and ignore the first line of w's output |
87 | 91 | s = s.strip().replace('|', '!').splitlines()[1:] |
| 92 | + if not s: |
| 93 | + return s, 0, 0 |
| 94 | + |
| 95 | + header = s[0] |
| 96 | + tty_start = header.find('TTY') |
| 97 | + if tty_start < 0: |
| 98 | + return s, 0, 0 |
| 99 | + |
| 100 | + # find end of TTY column: start of the next column header after TTY |
| 101 | + after_tty = header[tty_start + 3:] |
| 102 | + tty_end = tty_start + 3 + (len(after_tty) - len(after_tty.lstrip())) |
| 103 | + |
88 | 104 | count_tty, count_pts = 0, 0 |
89 | | - for line in s: |
90 | | - value = line.split()[1] |
91 | | - if 'tty' in value or ':' in value: |
92 | | - # for example, ":0", the 0. host display |
93 | | - # see https://unix.stackexchange.com/questions/16815/what-does-display-0-0-actually-mean |
| 105 | + for line in s[1:]: |
| 106 | + tty_value = line[tty_start:tty_end].strip() if len(line) > tty_start else '' |
| 107 | + if tty_value.startswith('tty') or tty_value.startswith(':'): |
| 108 | + # tty = local terminal, ":0" = local X display |
94 | 109 | count_tty += 1 |
95 | 110 | else: |
96 | | - # this always counts the first header line "USER TTY FROM ...", too |
| 111 | + # pts, empty (SSH without TTY) or anything else |
97 | 112 | count_pts += 1 |
98 | 113 |
|
99 | | - return s, count_tty, count_pts - 1 |
| 114 | + return s, count_tty, count_pts |
100 | 115 |
|
101 | 116 |
|
102 | 117 | def parse_windows_output(s): |
|
0 commit comments