File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/Linux/Videos.pm
Criterion Covered Total %
statement 40 71 56.3
branch 30 48 62.5
condition 0 18 0.0
subroutine 6 8 75.0
pod 0 2 0.0
total 76 147 51.7


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::Linux::Videos;
2              
3 2     2   47843868 use strict;
  2         15  
  2         82  
4 2     2   12 use warnings;
  2         9  
  2         101  
5              
6 2     2   1262 use FusionInventory::Agent::Tools;
  2         7  
  2         451  
7 2     2   1268 use FusionInventory::Agent::Tools::Unix;
  2         8  
  2         2308  
8              
9             sub isEnabled {
10 0     0 0 0 my (%params) = @_;
11 0 0       0 return 0 if $params{no_category}->{video};
12 0         0 return 1;
13             }
14              
15             sub doInventory {
16 0     0 0 0 my (%params) = @_;
17              
18 0         0 my $inventory = $params{inventory};
19 0         0 my $logger = $params{logger};
20              
21 0         0 $logger->debug("retrieving display information:");
22              
23 0         0 my $ddcprobeData;
24 0 0       0 if (canRun('ddcprobe')) {
25 0         0 $ddcprobeData = _getDdcprobeData(
26             command => 'ddcprobe',
27             logger => $logger
28             );
29 0         0 $logger->debug_result(
30             action => 'running ddcprobe command',
31             data => $ddcprobeData
32             );
33             } else {
34 0         0 $logger->debug_result(
35             action => 'running ddcprobe command',
36             status => 'command not available'
37             );
38             }
39              
40 0         0 my $xorgData;
41              
42             my $xorgPid;
43 0         0 foreach my $process (getProcesses(logger => $logger)) {
44 0 0       0 next unless $process->{CMD} =~ m{
45             ^
46             (?:
47             /usr/bin
48             |
49             /usr/X11R6/bin
50             |
51             /etc/X11
52             )
53             /X
54             }x;
55 0         0 $xorgPid = $process->{PID};
56 0         0 last;
57             }
58              
59 0 0       0 if ($xorgPid) {
60 0         0 my $link = "/proc/$xorgPid/fd/0";
61 0 0       0 if (-r $link) {
62 0         0 $xorgData = _parseXorgFd(file => $link);
63 0         0 $logger->debug_result(
64             action => 'reading Xorg log file',
65             data => $xorgData
66             );
67             } else {
68 0         0 $logger->debug_result(
69             action => 'reading Xorg log file',
70             status => "non-readable link $link"
71             );
72             }
73             } else {
74 0         0 $logger->debug_result(
75             action => 'reading Xorg log file',
76             status => 'unable to get Xorg PID'
77             );
78             }
79              
80 0 0 0     0 return unless $xorgData || $ddcprobeData;
81              
82             my $video = {
83             CHIPSET => $xorgData->{product} || $ddcprobeData->{product},
84             MEMORY => $xorgData->{memory} || $ddcprobeData->{memory},
85             NAME => $xorgData->{name} || $ddcprobeData->{oem},
86             RESOLUTION => $xorgData->{resolution} || $ddcprobeData->{dtiming},
87             PCISLOT => $xorgData->{pcislot},
88             PCIID => $xorgData->{pciid},
89 0   0     0 };
      0        
      0        
      0        
90              
91 0 0 0     0 if ($video->{MEMORY} && $video->{MEMORY} =~ s/kb$//i) {
92 0         0 $video->{MEMORY} = int($video->{MEMORY} / 1024);
93             }
94 0 0       0 if ($video->{RESOLUTION}) {
95 0         0 $video->{RESOLUTION} =~ s/@.*//;
96             }
97              
98             $inventory->addEntry(
99 0         0 section => 'VIDEOS',
100             entry => $video
101             );
102             }
103              
104             sub _getDdcprobeData {
105 7     7   20245 my $handle = getFileHandle(@_);
106 7 50       17 return unless $handle;
107              
108 7         40 my $data;
109 7         100 while (my $line = <$handle>) {
110 234         647 $line =~ s/[[:cntrl:]]//g;
111 234         378 $line =~ s/[^[:ascii:]]//g;
112 234 100       1437 $data->{$1} = $2 if $line =~ /^(\S+):\s+(.*)/;
113             }
114 7         53 close $handle;
115              
116 7         27 return $data;
117             }
118              
119             sub _parseXorgFd {
120 12     12   15805 my $handle = getFileHandle(@_);
121 12 50       31 return unless $handle;
122              
123 12         15 my $data;
124 12         194 while (my $line = <$handle>) {
125 9024 100       91696 if ($line =~ /Modeline\s"(\S+?)"/) {
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
126 848 100       4231 $data->{resolution} = $1 if !$data->{resolution};
127             } elsif ($line =~ /Integrated Graphics Chipset:\s+(.*)/) {
128             # Intel
129 4         23 $data->{name} = $1;
130             } elsif ($line =~ /Virtual screen size determined to be (\d+)\s*x\s*(\d+)/) {
131             # Nvidia
132 3         20 $data->{resolution} = "$1x$2";
133             } elsif ($line =~ /NVIDIA GPU\s*(.*?)\s*at/) {
134 3         15 $data->{name} = $1;
135             } elsif ($line =~ /VESA VBE OEM:\s*(.*)/) {
136 6         32 $data->{name} = $1;
137             } elsif ($line =~ /VESA VBE OEM Product:\s*(.*)/) {
138 6         27 $data->{product} = $1;
139             } elsif ($line =~ /VESA VBE Total Mem: (\d+)\s*(\w+)/i) {
140 6         38 $data->{memory} = $1 . $2;
141             } elsif ($line =~ /RADEON\(0\): Chipset: "(.*?)"/i) {
142             # ATI /Radeon
143 1         7 $data->{name} = $1;
144             } elsif ($line =~ /Virtual size is (\S+)/i) {
145             # VESA / XFree86
146 4         26 $data->{resolution} = $1;
147             } elsif ($line =~ /
148             PCI: \* \( (?:\d+:)? (\d+) : (\d+) : (\d+) \) \s
149             (\w{4}:\w{4}:\w{4}:\w{4})?
150             /x) {
151 12         90 $data->{pcislot} = sprintf("%02d:%02d.%d", $1, $2, $3);
152 12 100       82 $data->{pciid} = $4 if $4;
153             } elsif ($line =~ /NOUVEAU\(0\): Chipset: "(.*)"/) {
154             # Nouveau
155 1         7 $data->{product} = $1;
156             }
157             }
158 12         351 close $handle;
159              
160 12         60 return $data;
161             }
162              
163             1;