1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use 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 | |
---|
21 | For the default object_type (user), obj_id = login. |
---|
22 | |
---|
23 | Example : la-query lambda |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | GetOptions( |
---|
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 | |
---|
39 | Use this configuration directory instead of the default one. |
---|
40 | |
---|
41 | =back |
---|
42 | |
---|
43 | =cut |
---|
44 | |
---|
45 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
46 | my $labase = $LA->default_base; |
---|
47 | $labase && $labase->load or die "Cannot load base"; |
---|
48 | |
---|
49 | $labase->unexported(1); |
---|
50 | |
---|
51 | my $swdb = DBI->connect('dbi:Pg:host=pgsql.latmos.ipsl.fr;dbname=switchs', 'thauvin') |
---|
52 | or die "Can't connect to db"; |
---|
53 | |
---|
54 | my $listarp = $swdb->prepare(q{ |
---|
55 | select * from arp where lastview > now() - '6 months'::interval |
---|
56 | }); |
---|
57 | |
---|
58 | $listarp->execute(); |
---|
59 | my $res = $listarp->fetchall_hashref([ 'ip', 'mac' ]); |
---|
60 | |
---|
61 | my %host_seen = map { $_ => 0 } $labase->list_objects('nethost'); |
---|
62 | |
---|
63 | foreach 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 | |
---|
81 | foreach (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 | } |
---|