]> andersk Git - moira.git/blob - gen/ip-billing.gen
Include creation time for hosts; set it to NONE for private subnets.
[moira.git] / gen / ip-billing.gen
1 #!/moira/bin/perl -Tw
2
3 # $Id$
4 # The following exit codes are defined and MUST BE CONSISTENT with the
5 # error codes the library uses:
6 $MR_DBMS_ERR = 47836421;
7 $MR_OCONFIG = 47836460;
8
9 $ENV{PATH} = "/bin:/usr/bin:/sbin:/usr/sbin";
10
11 $outdir = '/moira/dcm/ip-billing';
12 $outfile = '/moira/dcm/ip-billing.out';
13 $outdat = '/moira/dcm/ip-billing/moira.dat';
14 $outctl = '/moira/dcm/ip-billing/moira.ctl';
15 $count = 0;
16
17 use DBI;
18 use Socket;
19
20 umask 022;
21 open(DAT, ">$outdat") || exit $MR_OCONFIG;
22 open(CTL, ">$outctl") || exit $MR_OCONFIG;
23
24 $dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira")
25   || exit $MR_DBMS_ERR;
26
27 # First report all the NETWK-A hosts.
28 $sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
29                      "m.account_number, m.created, m.modtime FROM " .
30                      "machine m, subnet s WHERE m.status = 1 " .
31                      "AND m.snet_id = s.snet_id AND s.status = 1 " .
32                      "AND m.address != 'unassigned' ORDER BY m.address") 
33     || exit $MR_DBMS_ERR;
34
35 $sth->execute || exit $MR_DBMS_ERR;
36
37 while (($address, $location, $name, $contact, $accountnumber, $created, $modtime) =
38        $sth->fetchrow_array) {
39     if ($location eq "\0") {
40         $location = "NONE";
41     }
42     if ($contact eq "\0") {
43         $contact = "NONE";
44     }
45     if ($accountnumber eq "\0") {
46         $accountnumber = "NONE";
47     }
48     $row = "$address\t$location\t$name\t$contact\t$accountnumber\t+NETWK-A\t$created\t$modtime\n";
49     $row =~ s/\0//g;
50     print DAT $row;
51     $count++;
52 }
53
54 # Next we do the private subnets.
55 # +NETWK-10P.
56 $sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
57                      "s.account_number, s.modtime FROM subnet s " .
58                      "WHERE s.status = 2 ORDER BY s.saddr")
59     || exit $MR_DBMS_ERR;
60
61 $sth->execute || exit $MR_DBMS_ERR;
62
63 while (($saddr, $name, $contact, $accountnumber, $modtime) =
64        $sth->fetchrow_array) {
65     if ($contact eq "\0") {
66         $contact = "NONE";
67     }
68     if ($accountnumber eq "\0") {
69         $accountnumber = "NONE";
70     }
71     @addr = unpack("C4", pack("N", $saddr));
72     $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
73     $row = "$address\tNONE\t$name\t$contact\t$accountnumber\t+NETWK-10P\tNONE\t$modtime\n";
74     $row =~ s/\0//g;
75     print DAT $row;
76     $count++;
77 }
78
79 # +NETWK-100P
80 $sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
81                      "s.account_number, s.modtime FROM subnet s " .
82                      "WHERE s.status = 3 ORDER BY s.saddr")
83     || exit $MR_DBMS_ERR;
84
85 $sth->execute || exit $MR_DBMS_ERR;
86
87 while (($saddr, $name, $contact, $accountnumber, $modtime) =
88        $sth->fetchrow_array) {
89     if ($contact eq "\0") {
90         $contact = "NONE";
91     }
92     if ($accountnumber eq "\0") {
93         $accountnumber = "NONE";
94     }
95     @addr = unpack("C4", pack("N", $saddr));
96     $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
97     $row = "$address\tNONE\t$name\t$contact\t$accountnumber\t+NETWK-100P\tNONE\t$modtime\n";
98     $row =~ s/\0//g;
99     print DAT $row;
100     $count++;
101 }
102
103 # +NETWK-1000P
104 $sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
105                      "s.account_number, s.modtime FROM subnet s " .
106                      "WHERE s.status = 7 ORDER BY s.saddr")
107     || exit $MR_DBMS_ERR;
108
109 $sth->execute || exit $MR_DBMS_ERR;
110
111 while (($saddr, $name, $contact, $accountnumber, $modtime) =
112        $sth->fetchrow_array) {
113     if ($contact eq "\0") {
114         $contact = "NONE";
115     }
116     if ($accountnumber eq "\0") {
117         $accountnumber = "NONE";
118     }
119     @addr = unpack("C4", pack("N", $saddr));
120     $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
121     $row = "$address\tNONE\t$name\t$contact\t$accountnumber\t+NETWK-1000P\tNONE\t$modtime\n";
122     $row =~ s/\0//g;
123     print DAT $row;
124     $count++;
125 }
126
127 # +NETWK-0
128 $sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
129                      "m.account_number, m.created, m.modtime FROM " .
130                      "machine m, subnet s WHERE m.status = 1 " .
131                      "AND m.snet_id = s.snet_id AND " .
132                      "(s.status = 4 OR s.status = 5 OR s.status = 6) " .
133                      "AND m.address != 'unassigned' ORDER BY m.address") 
134     || exit $MR_DBMS_ERR;
135
136 $sth->execute || exit $MR_DBMS_ERR;
137
138 while (($address, $location, $name, $contact, $accountnumber, $created, $modtime) =
139        $sth->fetchrow_array) {
140     if ($location eq "\0") {
141         $location = "NONE";
142     }
143     if ($contact eq "\0") {
144         $contact = "NONE";
145     }
146     if ($accountnumber eq "\0") {
147         $accountnumber = "NONE";
148     }
149     $row = "$address\t$location\t$name\t$contact\t$accountnumber\t+NETWK-0\t$created\t$modtime\n";
150     $row =~ s/\0//g;
151     print DAT $row;
152     $count++;
153 }
154
155 print CTL "$count\n";
156 close(CTL);
157 close(DAT);
158
159 system("cd $outdir && tar cf $outfile .") == 0 || exit $MR_OCONFIG;
160
161 $dbh->disconnect;
162
163 exit 0;
This page took 0.089375 seconds and 5 git commands to generate.