This file is indexed.

/usr/bin/systray-mdstat is in systray-mdstat 1.1.0-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

our $VERSION = '1.1.0'; # VERSION
# PODNAME: systray-mdstat
# ABSTRACT: system tray icon which shows the state of local MD RAIDs


use List::Util qw(max);
use File::ShareDir ':ALL';
use Try::Tiny;

my $proc_mdstat = '/proc/mdstat';
my @icon_dirs = qw(
    share
    );
my @states = qw( spare ok warn fail );
my @mdstat_checks = (
    # regular expression  => state => string
    [ qr/\[U+\]/                => 1     => 'OK'       ],
    [ qr/(resync|verify|check)/ => 2     => ' (%s)'    ],
    [ qr/\(F\)/                 => 3     => '<b>FAILED</b>'   ],
    [ qr/\[U*_U*\]/             => 3     => '<b>DEGRADED</b>' ],
    );

our $use_notify = 1;
my $old_state = 0;
my $icon;

main() unless caller(0);

sub main {
    use Getopt::Long qw( :config no_auto_abbrev );
    use Gtk3 -init;
    use Glib qw(TRUE FALSE);
    use Glib::Object::Introspection;
    use Pod::Usage;

    my $help;
    my $usage;
    GetOptions(
        'help|h'        => \$help,
        'usage'         => \$usage,
        'read-from=s'   => \$proc_mdstat,
        ) or exit 1;

    pod2usage(0) if $help;
    pod2usage(
            -verbose => 2,
            -exitval => 0,
        ) if $usage;

    if (@ARGV) {
        pod2usage(
            -message => "E: $0 takes no arguments\n",
            -exitval => 1,
            );
    }

    $icon = Gtk3::StatusIcon->new();
    $icon->set_visible(1);

    try {
        Glib::Object::Introspection->setup(
            basename => 'Notify',
            version => '0.7',
            package => "Systray::Mdstat::Notify",
            );
        Systray::Mdstat::Notify->init();
    } catch {
        say "no notify because setup failed: $@";
        $use_notify = 0;
    };

    populate_icon_dirs();

    check_mdstat();
    Glib::Timeout->add_seconds(3, \&check_mdstat);
    Gtk3->main();
}

sub populate_icon_dirs {
    # Unfortunately File::ShareDir functions die instead of returning
    # undef if they can't find an according directory.
    try {
        push(@icon_dirs, dist_dir('systray-mdstat'));
    };
}


sub read_and_parse_mdstat {
    # 0 = unknown
    # 1 = ok
    # 2 = warning
    # 3 = alarm
    my $state = 0;
    my $text = '';
    my $path = shift;

    my $last_md = '';
    if (-e $path) {
        open(my $MDSTAT, '<', $path)
            or die "Can't read from $path despite it exists: $!";
        while (my $line = <$MDSTAT>) {
            if ($line =~ /^(md\d+) *: /) {
                $last_md = $1;
                $text .= "; " unless $text eq '';
            } else {
                foreach my $check (@mdstat_checks) {
                    my $use_format = $check->[2] =~ /\%/;
                    if ($line =~ $check->[0]) {
                        $state = max($state, $check->[1]);
                        if ($use_format) {
                            $text .= sprintf($check->[2], $1);
                        } else {
                            $text .= "$last_md: ".$check->[2];
                        }
                    }
                }
            }
        }
        close $MDSTAT;
        $text = "$path exists but doesn't contain any RAID."
            unless $text;
    } else {
        $text = "No $path found";
    }

    return($text, $state);
}

sub check_mdstat {
    my ($text, $state) = read_and_parse_mdstat($proc_mdstat);
    &update_icon($icon, $text, $state);

    my $message;
    if ($old_state == 0 and $state != 0) {
        $message = "Current $proc_mdstat state: ".$text;
    } else {
        $message = "$proc_mdstat state changed: ".$text;
    }
    if ($state != $old_state) {
        &send_notification($icon, "Software RAID State", $message);
        $old_state = $state;
    }

    # Return explicitly true so that Gtk3 continues to run this
    # function.
    return TRUE;
}

sub update_icon {
    my ($icon, $text, $state) = @_;
    my $path = find_icon_path("harddrive".$states[$state]);

    if ($icon) {
        $icon->set_tooltip_text($text);
        $icon->set_from_file($path);
    } else {
        return "UPDATE ICON: text=$text path=$path";
    }
}

sub send_notification {
    my ($icon, $title, $text) = @_;

    # Check if we're running under a GUI by looking at $icon
    if ($icon) {
        if ($use_notify) {
            my $notif = Systray::Mdstat::Notify::Notification->new(
                $title, $text);
            try {
                $notif->show;
            } catch {
                # Something went wrong trying to show a notification
                # message. Fall back to using a dialog box instead.
                $use_notify = 0;
            };
        }
        if (!$use_notify) {
            my $dialog = Gtk3::MessageDialog->new(
                undef, 'destroy-with-parent', 'warning', 'ok', $text);
            $dialog->run;
            $dialog->destroy;
        }
    } else {
        return "NOTIFY: title=$title text=$text";
    }
}

sub find_icon_path {
    my $basename = shift;
    my $filename = "$basename.png";
    my $fullpath;
    foreach my $path (@icon_dirs) {
        if (-r "$path/$filename") {
            $fullpath = "$path/$filename";
            last;
        }
    }
    die "Icon $filename not found inside ".join(', ', @icon_dirs)
        unless $fullpath;
    return $fullpath;
}

__END__

=pod

=encoding UTF-8

=head1 NAME

systray-mdstat - system tray icon which shows the state of local MD RAIDs

=head1 VERSION

version 1.1.0

=head1 SYNOPSIS

B<systray-mdstat> [ B<-h> | B<--help> | B<--usage> ]

=head1 DESCRIPTION

This program allows one to display an icon indicating the state of
local Linux Software (MD) RAIDs in any freedesktop.org-compliant
status area.

=head1 SEE ALSO

L<smart-notifier(1)>

=head1 NOTES ABOUT THE ORIGIN OF THE CODE

Parts of the code are very loosely based on the outer framework of
fdpowermon by Wouter Verhelst under Poul-Henning Kamp's "Beer-ware"
license, and the mdstat check from Debian's hobbit-plugins package
written by Christoph Berg under the MIT license. (Both stated that the
amount of code I copied is too small to make their copyright apply,
hence I'm not bound to the licenses they used for their code.)

=head1 AUTHOR

Axel Beckert <abe@deuxchevaux.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2017 by Axel Beckert.

This is free software, licensed under:

  The GNU General Public License, Version 3, June 2007

=cut