Skip to content

Commit

Permalink
feat: Added support for installed softwares by Snap on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Nov 6, 2019
1 parent 6273aec commit 2e2cd1d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -24,6 +24,7 @@ inventory:
* win32: fix Office license inventory, thanks to PR-gh
* win32: enhanced network card inventory, thanks to PR-gh
* linux: provides debian installed software filesize in bytes
* linux: Added support for installed softwares by Snap

netdiscovery/netinventory:
* linux: avoid to share HTTP client with threads to fix RHEL/CentOS 7 crash
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -32,6 +32,7 @@ requires 'Text::Template' => '0';
requires 'UNIVERSAL::require' => '0';
requires 'XML::TreePP' => '0.26';
requires 'XML::XPath' => '0';
requires 'YAML::Tiny' => '0';

if ($OSNAME eq 'MSWin32') {
requires 'Win32::OLE' => '0';
Expand Down
107 changes: 107 additions & 0 deletions lib/FusionInventory/Agent/Task/Inventory/Generic/Softwares/Snap.pm
@@ -0,0 +1,107 @@
package FusionInventory::Agent::Task::Inventory::Generic::Softwares::Snap;

use strict;
use warnings;

use parent 'FusionInventory::Agent::Task::Inventory::Module';

use File::stat;
use YAML::Tiny;

use FusionInventory::Agent::Tools;

sub isEnabled {
return canRun('snap');
}

sub doInventory {
my (%params) = @_;

my $inventory = $params{inventory};
my $logger = $params{logger};

my $packages = _getPackagesList(
logger => $logger,
command => 'snap list --color never',
);
return unless $packages;

foreach my $snap (@{$packages}) {
_getPackagesInfo(
logger => $logger,
snap => $snap,
command => 'snap info --color never --abs-time '.$snap->{NAME},
);
$inventory->addEntry(
section => 'SOFTWARES',
entry => $snap
);
}
}

sub _getPackagesList {
my (%params) = @_;

my $handle = getFileHandle(%params);
return unless $handle;

my @packages;
while (my $line = <$handle>) {
chomp $line;
my @infos = split(/\s+/, $line)
or next;

# Skip header
next if $infos[0] eq 'Name' && $infos[1] eq 'Version';

# Skip base and snapd
next if $infos[5] && $infos[5] =~ /^base|snapd$/;

my $snap = {
NAME => $infos[0],
VERSION => $infos[1],
FROM => 'snap'
};

my $folder = "/snap/".$snap->{NAME};
if (-d $folder) {
my $st = stat($folder);
my ($year, $month, $day) = (localtime($st->mtime))[5, 4, 3];
$snap->{INSTALLDATE} = sprintf(
"%02d/%02d/%04d", $day, $month + 1, $year + 1900
);
}

push @packages, $snap,
}
close $handle;

return \@packages;
}

sub _getPackagesInfo {
my (%params) = @_;

my $snap = delete $params{snap};
my $lines = getAllLines(%params)
or return;

my $yaml = YAML::Tiny->read_string($lines);
my $infos = $yaml->[0]
or return;

return unless $infos->{name};

$snap->{PUBLISHER} = $infos->{publisher};
# Cleanup publisher from 'starred' if verified
$snap->{PUBLISHER} =~ s/[*]$//;
$snap->{COMMENTS} = $infos->{summary};
$snap->{HELPLINK} = $infos->{contact};

# Find installed size
my ($size) = $infos->{installed} =~ /\(.*\)\s+(\d+\S+)/;
$snap->{FILESIZE} = getCanonicalSize($size, 1024) * 1048576
if $size;
}

1;

0 comments on commit 2e2cd1d

Please sign in to comment.