habshi has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: nrpe + wmi problem
by roboticus (Chancellor) on Jun 19, 2011 at 14:56 UTC

    habshi:

    Your code is getting rows from the query, but prints "has no mapped drive". So perhaps the string returned for mapped drives under linux doesn't match "\\storage\shared"? If you print the value, you may see another string for mapped drives. (I don't use WMI so I don't know. But what if the reported drive under linux matched the unix path format, and reported something like "//storage/shared"?)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi, first thanks for reply.
      Second, I wrote the code wrong here, putted the if statement in wrong place(it's not original code, as the original has bit more stuff, but it's not relevant for this problem).
      I have fixed the code in the first post, but in case of that you have seen it would print nothing...
      I do think that the problem is some way related to the linux machine, but I don't understand how, as the script running on windows machine and just return the output to linux via nrpe...