source: LATMOS-Accounts/scripts/la-sw-net @ 861

Last change on this file since 861 was 861, checked in by nanardon, 13 years ago
  • reimport missing files from previous svn
  • Property svn:executable set to *
File size: 2.2 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use DBI;
9
10=head1 NAME
11
12    la-query - Tools to query base in LATMOS::Accounts system
13
14=head1 SYNOPSIS
15
16    la-query [options] [obj_id]
17
18=item [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id.
19        If none is given, all obj_ids will be printed.
20
21For the default object_type (user), obj_id = login.
22
23Example : la-query lambda
24
25=cut
26
27GetOptions(
28    'c|config=s'        => \my $config,
29    'help'              => sub { pod2usage(0) },
30) or pod2usage();
31
32
33=head1 OPTIONS
34
35=over 4
36
37=item -c|--config configdir
38
39Use this configuration directory instead of the default one.
40
41=back
42
43=cut
44
45my $LA = LATMOS::Accounts->new($config, noacl => 1);
46my $labase = $LA->default_base;
47$labase && $labase->load or die "Cannot load base";
48
49$labase->unexported(1);
50
51my $swdb = DBI->connect('dbi:Pg:host=pgsql.latmos.ipsl.fr;dbname=switchs', 'thauvin')
52    or die "Can't connect to db";
53
54my $listarp = $swdb->prepare(q{
55    select * from arp where lastview > now() - '6 months'::interval
56    });
57
58$listarp->execute();
59my $res = $listarp->fetchall_hashref([ 'ip', 'mac' ]);
60
61my %host_seen = map { $_ => 0 } $labase->list_objects('nethost');
62
63foreach my $ip (keys %{ $res }) {
64    my ($host) = $labase->search_objects('nethost', 'ip=' . $ip);
65    $host or next;
66    my $hosto = $labase->get_object('nethost', $host);
67    $host_seen{$host} = 1;
68    my %macs = map { $_ => 1 } grep { $_ } $hosto->get_attributes('macaddr');
69    foreach my $mac (keys %{ $res->{$ip} }) {
70        if ($macs{$mac}) {
71            next
72        };
73        my ($hostm) = $labase->search_objects('nethost', 'macaddr=' . $mac);
74        if ($hostm) {
75            $host_seen{$hostm} ||= 2;
76            print "$mac belong to $hostm (not $host -- $ip)\n";
77        }
78    }
79}
80
81foreach (sort keys %host_seen) {
82    $host_seen{$_} and next;
83    my $h = $labase->get_object('nethost', $_);
84    my $owner = $h->get_attributes('owner');
85    my $user = $labase->get_object('user', $owner);
86    my $desc = $h->get_attributes('description');
87    printf("%s%s%s\n",
88        $_,
89        ($desc ? " $desc" : ''),
90        ($user ? ' (' . $user->get_attributes('displayName') . ')' : '')
91    );
92}
Note: See TracBrowser for help on using the repository browser.