hi, i have a code that checks in wmi logged in sessions and then check if they have mounted network drive. the code almost works, but i have two problems with it.
my code check current sessions, and then check who has explorer process running. but for some reasons, when i run it in windows, sometimes it doesn't return all sessions. the second problem is when i try to run this script with nrpe from nagios(linux machine). in this case i get all the sessions, but non of them can find any mapped network drives(and they exists). when i run it from windows it finds who has drives mapped and who hasn't.
here is the code:
use warnings ; use strict ; use Data::Dumper ; use DBI; my $disk_status = 0; my $dbh = DBI->connect('dbi:WMI:11.0.0.1'); my $sessions = $dbh->prepare("SELECT * FROM Win32_LogonSession WHERE L +ogonType<>3 AND LogonType<>5 AND LogonType<>0 AND LogonType<>8 AND Lo +gonType<>9 AND LogonType<>4") or die "Failed get sessions object\n"; $sessions->execute(); while (my @session_row = $sessions->fetchrow) { my $live_session = 0; my $disk_flag = 0; my $session = $session_row[0]; my $session_id = $session->{LogonId}; my $processes = $dbh->prepare("Associators of {Win32_LogonSession. +LogonId=".$session_id."} Where AssocClass=Win32_SessionProcess Role=A +ntecedent") or die "Failed get process object\n"; $processes->execute(); while (my @process_row = $processes->fetchrow) { my $process = $process_row[0]; my $process_name = $process->{Caption}; if ($process_name =~ /^explorer.exe$/) { $live_session = 1; } } next if (!$live_session); my $users = $dbh->prepare("Associators of {Win32_LogonSession.Logo +nId=".$session_id."} Where AssocClass=Win32_LoggedOnUser Role=Depende +nt") or die "Failed get users object\n"; $users->execute(); while (my @user_row = $users->fetchrow) { my $user = $user_row[0]; my $user_name = $user->{Name}; my $mapped_disks = $dbh->prepare("Associators of {Win32_LogonS +ession.LogonId=".$session_id."} Where AssocClass=Win32_LogonSessionMa +ppedDisk Role=Antecedent") or die "Failed get mapped disks object\n"; $mapped_disks->execute(); while (my @mapped_disks_row = $mapped_disks->fetchrow) { my $mapped_disk = $mapped_disks_row[0]; my $disk_uri = $mapped_disk->{ProviderName}; if ($disk_uri =~ /\\\\storage\\shared/) { $disk_flag = 1; } } } if ($disk_flag == 1) { print $user_name." has mapped drive\n"; } else { print $user_name." has no mapped drive\n"; } $disk_flag = 0; }
here is output from windows:
user1 has mapped drive
user2 has no mapped drive


here us output from linux(via nrpe)
user1 has no mapped drive
user2 has no mapped drive
user3 has no mapped drive


while user1 and user3 has mapped drive...

hope i wrote it understandable enough...
any help is welcomed!

thanks

In reply to nrpe + wmi problem by habshi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.