Skip to content

Commit

Permalink
Merge pull request #232 from anarcat/ubatch
Browse files Browse the repository at this point in the history
also output user sessions in batch mode
  • Loading branch information
liske authored Jan 27, 2022
2 parents e617625 + 1ff1bf4 commit 124cb3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ NEEDRESTART-KSTA: 1
NEEDRESTART-SVC: systemd-journald.service
NEEDRESTART-SVC: systemd-machined.service
NEEDRESTART-CONT: LXC web1
NEEDRESTART-SESS: metabase @ user manager service
NEEDRESTART-SESS: root @ session #28017
```

Batch mode can be used to use the results of needrestart in other scripts.
Expand Down
43 changes: 28 additions & 15 deletions needrestart
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,27 @@ if(scalar @easy_hints) {
$ui->announce_ehint(EHINT => ($h ? join(' ', $h, __ 'and', '') : '') . $t);
}

my @sessions_list;
if(scalar %sessions) {
# build a sorted list of user @ session strings
#
# used in the nagios and batch outputs below
@sessions_list = map {
my $uid = $_;
my $user = uid2name($uid);
my @ret;

foreach my $sess (sort keys %{ $sessions{$uid} }) {
push(@ret, "$user \@ $sess");
}

@ret;
}
sort {
ncmp(uid2name($a), uid2name($b));
} keys %sessions
}

# nagios plugin output
if($opt_p) {
my %states = (
Expand Down Expand Up @@ -1306,22 +1327,14 @@ if($opt_p) {
}

if(scalar %sessions) {
print "Sessions:", join("\n- ", '',
map {
my $uid = $_;
my $user = uid2name($uid);
my @ret;

foreach my $sess (sort keys %{ $sessions{$uid} }) {
push(@ret, "$user \@ $sess");
}

@ret;
}
sort {
ncmp(uid2name($a), uid2name($b));
} keys %sessions), "\n";
print "Sessions:", join("\n- ", '', @sessions_list), "\n";
}

exit $ret;
}

if ($opt_b and scalar %sessions) {
for my $sess (@sessions_list) {
print "NEEDRESTART-SESS: $sess\n";
}
}

0 comments on commit 124cb3f

Please sign in to comment.