File: CheckProcessor.pm

package info (click to toggle)
fusioninventory-agent 1%3A2.3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,636 kB
  • ctags: 1,451
  • sloc: perl: 89,223; xml: 422; sh: 83; python: 26; makefile: 22
file content (115 lines) | stat: -rw-r--r-- 3,146 bytes parent folder | download | duplicates (2)
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
package FusionInventory::Agent::Task::Deploy::CheckProcessor;

use strict;
use warnings;

use English qw(-no_match_vars);
use Digest::SHA;

use FusionInventory::Agent::Task::Deploy::DiskFree;

sub process {
    my ($self, %params) = @_;

    # the code to return in case of failure of the check,
    # the default is 'ko'
    my $failureCode = $params{check}->{return} || "ko";

    my $path = $params{check}->{path};

    # Expend the env vars from the path
    $path =~ s#\$(\w+)#$ENV{$1}#ge;
    $path =~ s#%(\w+)%#$ENV{$1}#ge;

    if ($params{check}->{type} eq 'winkeyExists') {
        return unless $OSNAME eq 'MSWin32';
        require FusionInventory::Agent::Tools::Win32;

        my $path = $path;
        $path =~ s{\\}{/}g;

        my $r = FusionInventory::Agent::Tools::Win32::getRegistryKey(path => $path);

        return defined $r ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'winkeyEquals') {
        return unless $OSNAME eq 'MSWin32';
        require FusionInventory::Agent::Tools::Win32;

        $path =~ s{\\}{/}g;

        my $r = FusionInventory::Agent::Tools::Win32::getRegistryValue(path => $path);

        return defined $r && $params{check}->{value} eq $r ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'winkeyMissing') {
        return unless $OSNAME eq 'MSWin32';
        require FusionInventory::Agent::Tools::Win32;

        my $path = $path;
        $path =~ s{\\}{/}g;

        my $r = FusionInventory::Agent::Tools::Win32::getRegistryKey(path => $path);

        return defined $r ? $failureCode : 'ok';
    }

    if ($params{check}->{type} eq 'fileExists') {
        return -f $path ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'fileSizeEquals') {
        my @s = stat($path);
        return @s ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'fileSizeGreater') {
        my @s = stat($path);
        return $failureCode unless @s;

        return $params{check}->{value} < $s[7] ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'fileSizeLower') {
        my @s = stat($path);
        return $failureCode unless @s;
        return $params{check}->{value} > $s[7] ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'fileMissing') {
        return -f $path ? $failureCode : 'ok';
    }

    if ($params{check}->{type} eq 'freespaceGreater') {
        my $freespace = getFreeSpace(logger => $params{logger}, path => $path);
        return $freespace>$params{check}->{value}? "ok" : $failureCode;
    }

    if ($params{check}->{type} eq 'fileSHA512') {
        my $sha = Digest::SHA->new('512');

        my $sha512 = "";
        eval {
            $sha->addfile($path, 'b');
            $sha512 = $sha->hexdigest;
        };

        return $sha512 eq $params{check}->{value} ? "ok" : $failureCode;
    }

    if ($params{check}->{type} eq 'directoryExists') {
        return -d $path ? 'ok' : $failureCode;
    }

    if ($params{check}->{type} eq 'directoryMissing') {
        return -d $path ? $failureCode : 'ok';
    }

    print "Unknown check: `".$params{check}->{type}."'\n";

    return "ok";
}

1;