source: repository/lib/Metabrik/Remote/Wmi.pm

METABRIK_REPOSITORY_1_26_0
Last change on this file was 873:94e85541d72a, checked in by GomoR <gomor@…>, 8 months ago
  • version: 1.26.0
  • bugfix: brik::tool: install Command to call new_from_brik_no_checks Command to avoid errors when program to install is not yet installed :)
File size: 5.2 KB
Line 
1#
2# $Id$
3#
4# remote::wmi Brik
5#
6package Metabrik::Remote::Wmi;
7use strict;
8use warnings;
9
10use base qw(Metabrik::Shell::Command Metabrik::System::Package);
11
12sub brik_properties {
13   return {
14      revision => '$Revision$',
15      tags => [ qw(unstable) ],
16      author => 'GomoR <GomoR[at]metabrik.org>',
17      license => 'http://opensource.org/licenses/BSD-3-Clause',
18      attributes => {
19         datadir => [ qw(datadir) ],
20         host => [ qw(host) ],
21         user => [ qw(username) ],
22         password => [ qw(password) ],
23      },
24      attributes_default => {
25         as_array => 1,
26         capture_mode => 1,
27         use_globbing => 0,
28      },
29      commands => {
30         install => [ ], # Inherited
31         request => [ qw(query host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
32         get_win32_operatingsystem => [ qw(host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
33         get_win32_process => [ qw(host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
34      },
35      require_modules => {
36         'Metabrik::Client::Www' => [ ],
37         'Metabrik::System::File' => [ ],
38         'Metabrik::String::Psv' => [ ],
39      },
40      require_binaries => {
41         tar => [ ],
42         wmic => [ ],
43      },
44      need_packages => {
45         ubuntu => [ qw(build-essential autoconf) ],
46         debian => [ qw(build-essential autoconf) ],
47      },
48   };
49}
50
51#
52# Compilation process
53# http://techedemic.com/2014/09/17/installing-wmic-in-ubuntu-14-04-lts-64-bit/
54# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
55#
56sub install {
57   my $self = shift;
58
59   # Install needed packages
60   $self->SUPER::install() or return;
61
62   my $datadir = $self->datadir;
63   my $shell = $self->shell;
64
65   my $version = '1.3.14';
66
67   my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
68   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
69   my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
70
71   if (@$files > 0) {
72      my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
73      $self->SUPER::execute($cmd) or return;
74   }
75
76   # cd wmi-$version/Samba/source
77   # ./autogen.sh
78   # ./configure
79   # On Ubuntu 16.10, we have to comment line 583 from wmi-1.3.14/Samba/source/pidl/pidl
80   #   Error message was:
81   #   Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at ./pidl/pidl line 583.
82   #   Makefile:28886: recipe for target 'idl' failed
83   # make "CPP=gcc -E -ffreestanding"
84   # make proto bin/wmic
85   # make proto bin/winexe
86
87   my $cwd = $shell->pwd;
88   $shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
89
90   $self->system('./autogen.sh') or return;
91   $self->system('./configure') or return;
92   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
93   $self->system('make proto bin/wmic') or return;
94   $self->system('make proto bin/winexe') or return;
95
96   $shell->run_cd($cwd);
97
98   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
99   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
100   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
101
102   return 1;
103}
104
105#
106# Must add specific user everywhere
107#
108# Howto enable WMI on a Windows machine
109# http://community.zenoss.org/docs/DOC-4517
110#
111# Troubleshoot WMI connexion issues:
112# wbemtest.exe + https://msdn.microsoft.com/en-us/library/windows/desktop/aa394603(v=vs.85).aspx
113#
114# dcomcnfg => DCOM permission for user
115# Computer/Manage/Properties => 'WMI Control/Properties/Security'
116#
117# Open firewall for DCOM service
118# http://www.returnbooleantrue.com/2014/10/enabling-wmi-on-windows-azure.html
119#
120sub request {
121   my $self = shift;
122   my ($query, $host, $user, $password) = @_;
123
124   $host ||= $self->host;
125   $user ||= $self->user;
126   $password ||= $self->password;
127   $self->brik_help_run_undef_arg('request', $query) or return;
128   $self->brik_help_run_undef_arg('request', $host) or return;
129   $self->brik_help_run_undef_arg('request', $user) or return;
130   $self->brik_help_run_undef_arg('request', $password) or return;
131
132   my $cmd = "wmic -U$user".'%'."$password //$host \"$query\"";
133
134   my $r = $self->SUPER::execute($cmd) or return;
135   #return $r;
136   if (@$r > 1) {
137      # First line is useless for us. Example: "CLASS: Win32_OperatingSystem"
138      shift @$r;
139      my $sp = Metabrik::String::Psv->new_from_brik_init($self) or return;
140      $sp->first_line_is_header(1);
141      # Need to desactivate double-quote parsing we may find in a process name
142      $sp->quote("'");
143      my $data = join("\n", @$r);
144      return $sp->decode($data);
145   }
146
147   return $r;
148}
149
150#
151# More requests:
152# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
153#
154sub get_win32_operatingsystem {
155   my $self = shift;
156
157   return $self->request('SELECT * FROM Win32_OperatingSystem', @_);
158}
159
160sub get_win32_process {
161   my $self = shift;
162
163   return $self->request('SELECT * FROM Win32_Process', @_);
164}
165
1661;
167
168__END__
169
170=head1 NAME
171
172Metabrik::Remote::Wmi - remote::wmi Brik
173
174=head1 COPYRIGHT AND LICENSE
175
176Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
177
178You may distribute this module under the terms of The BSD 3-Clause License.
179See LICENSE file in the source distribution archive.
180
181=head1 AUTHOR
182
183Patrice E<lt>GomoRE<gt> Auffret
184
185=cut
Note: See TracBrowser for help on using the repository browser.