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

Last change on this file was 942:170e9944f7e0, checked in by GomoR <gomor@…>, 6 weeks ago
  • UPDATE: *: debug base Attribute has been removed. Update all Briks by removing $self->debug.
  • UPDATE: *: global, context, shell base Attributes are no more initialized with a default Brik, they may now be undefined. Update all Briks by checking definedness when $self->global, $self->context or $self->shell are used.
File size: 5.5 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
64   my $version = '1.3.14';
65
66   my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
67   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
68   my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
69
70   if (@$files > 0) {
71      my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
72      $self->SUPER::execute($cmd) or return;
73   }
74
75   # cd wmi-$version/Samba/source
76   # ./autogen.sh
77   # ./configure
78   # On Ubuntu 16.10, we have to comment line 583 from wmi-1.3.14/Samba/source/pidl/pidl
79   #   Error message was:
80   #   Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at ./pidl/pidl line 583.
81   #   Makefile:28886: recipe for target 'idl' failed
82   # make "CPP=gcc -E -ffreestanding"
83   # make proto bin/wmic
84   # make proto bin/winexe
85
86   my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
87   if (defined($self->shell)) {
88      $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
89   }
90   else {
91      chdir("$datadir/wmi-$version/Samba/source")
92         or return $self->log->error("install: chdir: $!");
93   }
94
95   $self->system('./autogen.sh') or return;
96   $self->system('./configure') or return;
97   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
98   $self->system('make proto bin/wmic') or return;
99   $self->system('make proto bin/winexe') or return;
100
101   if (defined($self->shell)) {
102      $self->shell->run_cd($cwd);
103   }
104   else {
105      chdir($cwd) or return $self->log->error("install: chdir: $!");
106   }
107
108   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
109   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
110   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
111
112   return 1;
113}
114
115#
116# Must add specific user everywhere
117#
118# Howto enable WMI on a Windows machine
119# http://community.zenoss.org/docs/DOC-4517
120#
121# Troubleshoot WMI connexion issues:
122# wbemtest.exe + https://msdn.microsoft.com/en-us/library/windows/desktop/aa394603(v=vs.85).aspx
123#
124# dcomcnfg => DCOM permission for user
125# Computer/Manage/Properties => 'WMI Control/Properties/Security'
126#
127# Open firewall for DCOM service
128# http://www.returnbooleantrue.com/2014/10/enabling-wmi-on-windows-azure.html
129#
130sub request {
131   my $self = shift;
132   my ($query, $host, $user, $password) = @_;
133
134   $host ||= $self->host;
135   $user ||= $self->user;
136   $password ||= $self->password;
137   $self->brik_help_run_undef_arg('request', $query) or return;
138   $self->brik_help_run_undef_arg('request', $host) or return;
139   $self->brik_help_run_undef_arg('request', $user) or return;
140   $self->brik_help_run_undef_arg('request', $password) or return;
141
142   my $cmd = "wmic -U$user".'%'."$password //$host \"$query\"";
143
144   my $r = $self->SUPER::execute($cmd) or return;
145   #return $r;
146   if (@$r > 1) {
147      # First line is useless for us. Example: "CLASS: Win32_OperatingSystem"
148      shift @$r;
149      my $sp = Metabrik::String::Psv->new_from_brik_init($self) or return;
150      $sp->first_line_is_header(1);
151      # Need to desactivate double-quote parsing we may find in a process name
152      $sp->quote("'");
153      my $data = join("\n", @$r);
154      return $sp->decode($data);
155   }
156
157   return $r;
158}
159
160#
161# More requests:
162# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
163#
164sub get_win32_operatingsystem {
165   my $self = shift;
166
167   return $self->request('SELECT * FROM Win32_OperatingSystem', @_);
168}
169
170sub get_win32_process {
171   my $self = shift;
172
173   return $self->request('SELECT * FROM Win32_Process', @_);
174}
175
1761;
177
178__END__
179
180=head1 NAME
181
182Metabrik::Remote::Wmi - remote::wmi Brik
183
184=head1 COPYRIGHT AND LICENSE
185
186Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
187
188You may distribute this module under the terms of The BSD 3-Clause License.
189See LICENSE file in the source distribution archive.
190
191=head1 AUTHOR
192
193Patrice E<lt>GomoRE<gt> Auffret
194
195=cut
Note: See TracBrowser for help on using the repository browser.