Device Inventory

By Age By Model By OS By Location Wireless SSID VLAN

By Age

% $m->comp('SELF:show_age') if $cmd eq 'age';
Find Devices:
Find Devices:
that have been up for at least

By Model

% my @m = sort { % # Sort by vendor then model % return $a->{vendor} cmp $b->{vendor} if $a->{vendor} ne $b->{vendor}; % return lc($a->{model}) cmp lc($b->{model}); % } @$model_count; % $count = 0; $odd = 1; % my $number = scalar @m; % my $part2 = [splice(@m, int($number/2),$number)]; % my $part1 = [splice(@m, 0, int($number/2))]; % foreach my $part ($part1,$part2) { % }
<%perl> foreach my $row (@$part) { next unless defined($row); # Change class if we change vendor if ($old_vendor ne $row->{vendor}){ $odd++; $old_vendor = $row->{vendor}; } $count += $row->{count}; % }
Vendor Model Count
<%$row->{vendor} || 'Unknown'|h%> <%$row->{model} || 'Unknown'|h%> <%$row->{count}%>
Total: <%$count%>

By OS

% @m = sort { % # Sort by OS then Version % return $a->{os} cmp $b->{os} if $a->{os} ne $b->{os}; % return $a->{os_ver} cmp $b->{os_ver}; % } @$os_count; % $count = 0; $odd = 1; % $number = scalar @m; % $part2 = [splice(@m, int($number/2),$number)]; % $part1 = [splice(@m, 0, int($number/2))]; % foreach my $part ($part1,$part2) { % }
<%perl> $odd = 1; my $old_os = ''; foreach my $row (@$part) { next unless defined($row); if ($old_os ne $row->{os}) { $odd++; $old_os = $row->{os}; } $count += $row->{count}; # need either os or os_ver defined next unless defined $row->{os} or defined $row->{os_ver}; % }
OS Version Count
<% $row->{os} || 'Unknown' |h%> <% $row->{os_ver} || 'Unknown' |h%> <% $row->{count} %>
Total: <%$count%>

By Location

% if ($show_loc) { <& SELF:show_loc &> % } else { Show Inventory by Location % }

Wireless SSID Inventory

% if ($show_ssid) { <& SELF:show_ssid &> % } else { Show Wireless SSID Inventory % }

VLAN Inventory

% if ($show_vlan) { <& SELF:show_vlan &> % } else { Show VLAN Inventory % } <%args> $cmd => undef $age_type => 'last' $age_val => 2 $age_mul => 'months' $age_bool => 'not in' $show_loc => 0 $show_ssid => 0 $show_vlan => 0 %# %# %# <%shared> my $age = []; my $domain = $netdisco::CONFIG{domain}; my $odd = 1; my ($arg_cmd,$arg_age_type,$count,$arg_show_loc); %# %# %# <%init> $arg_cmd = $cmd; $arg_age_type = $age_type; $arg_show_loc = $show_loc; my $old_vendor = ''; my $model_count = sql_rows('device',['vendor','model','count(*)'], undef,undef,'GROUP by model,vendor'); my $os_count = sql_rows('device',['os','os_ver','count(*)'], undef,undef,'GROUP by os,os_ver'); if ($cmd eq 'age'){ # prevent stuffing $age_val = 3 unless $age_val =~ m/^\d+$/; $age_mul =~ s/[^a-z ]+//g; my $bool = '>='; $bool = '<=' if $age_bool =~ /last/; # Default - Last Discovered my $col = 'age(now(),last_discover)'; my $val = "$bool interval '$age_val $age_mul'"; # First Created if ($age_type eq 'first'){ $col = 'age(now(),creation)'; $val = "$bool interval '$age_val $age_mul'"; } # Uptime elsif ($age_type eq 'uptime') { $col = 'uptime'; my $interval = 0; my $mult = 60*60*24; # 1 day in seconds $mult = $mult * 30 if ($age_mul eq 'months'); $mult = $mult * 365 if ($age_mul eq 'years'); $mult *= 100; # uptime is in 100ths of seconds $interval = $age_val * $mult; $val = "$bool $interval" } $age = sql_rows('device',['ip','dns','name','vendor','model','uptime', 'age(now(),creation) as creation_age', 'age(now(),last_discover) as last_discover_age' ], {$col => \\$val} ); } %# %# show_age() - Process by age requests %# <%method show_age>

Results

% unless (scalar @$age) { No Results Found. % return; } <%perl> my @results; if ($arg_age_type eq 'uptime') { @results = sort { $b->{uptime} <=> $a->{uptime} } @$age; } else { @results = sort { return $a->{dns} cmp $b->{dns} unless (! defined($a->{dns}) or ! defined $b->{dns}); return $a->{ip} cmp $b->{ip}; } @$age; } $odd = 1; $count = 0; foreach my $dev (@results) { my $dns = $dev->{dns} || 'No DNS'; $dns =~ s/\Q$domain\E//; my $creation_age = $m->comp("SELF:.trim_age", age => $dev->{creation_age}); my $last_discover_age = $m->comp("SELF:.trim_age", age => $dev->{last_discover_age}); my $uptime = $m->comp('device.html:uptime',uptime => $dev->{uptime}); $odd++; % }
Device IP Vendor - Model <%$arg_age_type eq 'uptime' ? 'Uptime' : 'First
Discovered'%>
Last
Updated
<%$dns|h%> <%$dev->{ip}|h%> <%$dev->{vendor} || "Unknown"%> - <%$dev->{model} || "Unknown"|h%> <%$arg_age_type eq 'uptime' ? $uptime : $creation_age|h%> <%$last_discover_age|h%>
Found <%scalar @$age%> devices. %# %# .trim_age(age=>string) - Trim postgres ugly age string %# <%method .trim_age> <%args> $age => '' <%perl> $age =~ s/mon/month/; if ($age =~ /(day|month|year)/){ $age =~ s/(-)?\d{2}:\d{2}.*$//; } else { $age =~ s/(\d{2}:\d{2}):\d{2}(\.\d*)$/$1/; } return $age; %# %# show_ssid() - Inventory based on SSID %# <%method show_ssid> <%perl> my $ssid_count = sql_rows('device_port_ssid',['ssid','broadcast','count(ssid)'], undef,undef,'group by ssid,broadcast'); $count = 0; $odd = 1; my $old_loc = ''; foreach my $row (sort { $b->{count} <=> $a->{count} } @$ssid_count) { $odd++; % }
SSID Broadcast? Count
<%$row->{ssid}|h%> <%$row->{broadcast} ? "Broadcast" : "Stealth"%> <%$row->{count}%>
Hide %# %# show_vlan() - Inventory based on VLAN %# <%method show_vlan> <%perl> my $vlan_count = sql_rows('device_vlan',['vlan','description','count(vlan)'], undef,undef,'group by vlan,description'); my @m = sort { $a->{vlan} <=> $b->{vlan} || $a->{description} cmp $b->{description} } @$vlan_count; $count = 0; $odd = 1; my $old_vlan = -1; % my $m_half = int(scalar @m / 2); % foreach my $h (0,1) { % }
% foreach my $row (@m[($m_half*$h) .. ($m_half*($h+1)-1)]) { % # Change color if we change VLANs % if ($row->{vlan} != $old_vlan) { % $old_vlan = $row->{vlan}; % $odd++; % } % }
VLAN ID VLAN Name Count
<%$row->{vlan} |h%> [ports] <%$row->{description}%> <%$row->{count}%>
Total: <%scalar @m%> Hide %# %# show_loc() - Sorts and shows by location %# <%method show_loc> <%perl> my $location_count = sql_rows('device',['location','dns','name','ip','vendor','model'], undef,undef,undef); $count = 0; $odd = 0; my $old_loc = ''; foreach my $row (sort { # Sort by Location => Name => IP Address => Vendor => Model return $a->{location} cmp $b->{location} if $a->{location} ne $b->{location}; return $a->{name} cmp $b->{name} if $a->{name} ne $b->{name}; return $a->{ip} cmp $b->{ip} if $a->{ip} ne $b->{ip}; return $a->{vendor} cmp $b->{vendor} if $a->{vendor} ne $b->{vendor}; return $a->{model} cmp $b->{model} if $a->{model} ne $b->{model}; } @$location_count ) { $count += $row->{count}; $odd++ if $row->{location} ne $old_loc; $old_loc = $row->{location}; my $dns = $row->{dns} || $row->{ip}; $dns =~ s/\Q$domain\E//; % } $odd++;
Location IP Address sysName Vendor Model
<%$row->{location} || '[Not Set]'%> <%$dns|h%> <%$row->{name}|h%> <%$row->{vendor} || "Unknown"|h%> <%$row->{model} || "Unknown"|h%>
  Total: <%scalar(@$location_count)%>
Hide %# %# title() %# <%method title> - Device Inventory \ %# $Id: device_inv.html,v 1.31 2009/06/11 00:22:33 maxbaker Exp $ %# vim:syntax=mason