File: Drives.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.0.5-1.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,120 kB
  • ctags: 899
  • sloc: perl: 20,687; sh: 576; objc: 468; ansic: 333; makefile: 55
file content (149 lines) | stat: -rw-r--r-- 4,127 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
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
package Ocsinventory::Agent::Backend::OS::Linux::Drives;

use strict;
sub check {
  return unless can_run ("df");
  my $df = `df -TP`;
  return 1 if $df =~ /\w+/;
  0
}

sub run {
  my $params = shift;
  my $common = $params->{common};

  my %months = (
    Jan => 1,
    Fev => 2,
    Mar => 3,
    Apr => 4,
    May => 5,
    Jun => 6,
    Jul => 7,
    Aug => 8,
    Sep => 9,
    Oct => 10,
    Nov => 11,
    Dec => 12,
  );

  my %listVolume = ();
   
  # Get complementary information in hash tab
  if (can_run ("lshal")) {
    my %temp;
    my $in = 0;
    my $value;
    foreach my $line (`lshal`) {
      chomp $line;
      if ( $line =~ s{^udi = '/org/freedesktop/Hal/devices/volume.*}{}) {
        $in = 1;
        %temp = ();
      } elsif ($in == 1 and $line =~ s{^\s+(\S+) = (.*) \s*\((int|string|bool|string list|uint64)\)}{} ) {
        if ($3 ne 'int' and $3 ne 'uint64') {
          chomp($value = $2);
          if ($3 eq 'string') { 
            chop($value); 
            #$value =~ s/^'?(.*)'?$/$1/g;
            $value=substr($value,1,length($value));
            $value=substr($value,0,length($value)-1);
          }
          
          $temp{$1} = $value;
        } else {
          $temp{$1} = (split(/\W/,$2))[0];
        }
      }elsif ($in== 1 and $line eq '') {
        $in = 0 ;
        $listVolume{$temp{'block.device'}} = {%temp};
      }
    }
  }
  
  foreach(`df -TP`) { # TODO retrive error
    my $createdate;
    my $free;
    my $filesystem;
    my $label;
    my $total;
    my $type;
    my $volumn;
    my $serial;

    if(/^(\S+)\s+(\S+)\s+(\S+)\s+(?:\S+)\s+(\S+)\s+(?:\S+)\s+(\S+)\n/){
      $free = sprintf("%i",($4/1024));
      $filesystem = $2;
      $total = sprintf("%i",($3/1024));
      $type = $5;
      $volumn = $1;

# no virtual FS
      next if ($filesystem =~ /^(tmpfs|usbfs|proc|devpts|devshm|udev)$/);
      next if ($type =~ /^(tmpfs)$/);

      if ($filesystem =~ /^ext(2|3|4|4dev)/ && can_run('dumpe2fs')) {
        foreach (`dumpe2fs -h $volumn 2> /dev/null`) {
          if (/Filesystem UUID:\s+(\S+)/) {
            $serial = $1;
          } elsif (/Filesystem created:\s+\w+ (\w+) (\d+) ([\d:]+) (\d{4})$/) {
            $createdate = $4.'/'.$months{$1}.'/'.$2.' '.$3;
          } elsif (/Filesystem volume name:\s*(\S.*)/) {
            $label = $1 unless $1 eq '<none>';
          }
        }
      } elsif ($filesystem =~ /^xfs$/ && can_run('xfs_db')) {
        foreach (`xfs_db -r -c uuid $volumn`) {
          $serial = $1 if /^UUID =\s+(\S+)/;
            ;
          }
        foreach (`xfs_db -r -c label $volumn`) {
          $label = $1 if /^label =\s+"(\S+)"/;
        }
      } elsif ($filesystem =~ /^vfat$/ && can_run('dosfslabel')) {
          chomp ($label = `dosfslabel $volumn`);
      }

      $label =~ s/\s+$//;
      $serial =~ s/\s+$//;

      
      # Check information and improve it
      if (keys %listVolume) {
        if ( defined $listVolume{$volumn} ) {
          if ($filesystem eq '')  { $filesystem = $listVolume{$volumn}->{'volume.fstype'};}
          if ($label eq '')       { $label = $listVolume{$volumn}->{'volume.label'};}
          if ($total eq '')       { $total = int($listVolume{$volumn}->{'volume.size'}/(1024*1024) + 0.5);}
          if ($type eq '')        { $type = $listVolume{$volumn}->{'storage.model'};}
          if ($serial eq '')      { $serial = $listVolume{$volumn}->{'volume.uuid'};}
          delete ($listVolume{$volumn});
        }
      }

      $common->addDrive({
      	  CREATEDATE => $createdate,
          FREE => $free,
          FILESYSTEM => $filesystem,
          LABEL => $label,
          TOTAL => $total,
          TYPE => $type,
          VOLUMN => $volumn,
          SERIAL => $serial
        });
    }
  }

  if (can_run ("lshal")) {
    while (my ($k,$v) = each %listVolume ) {
      $common->addDrive({
        FILESYSTEM => $v->{'volume.fstype'},
        LABEL => $v->{'volume.label'},
        TOTAL => int ($v->{'volume.size'}/(1024*1024) + 0.5),
        TYPE => $v->{'storage.model'},
        VOLUMN => $k,
        SERIAL => $v->{'volume.uuid'}
      });
    }
  }  
}

1;