Yenta/Kibitz/Status.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 18 Conditioncond n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 123 CC 10 SCAR 23.8
CC
10
Cov
0%
update
30.0
update_sceptical
20.0
_myself
6.0
CRAP 10.8
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Mar-30 10:20 (EDT)
6 # Function: exchange status info via gossip protocol
7 #
8 # $Id: Status.pm,v 1.16 2010/09/07 15:31:56 jaw Exp $
9  
10 package AC::Yenta::Kibitz::Status;
11 use AC::Yenta::Kibitz::Status::Server;
12 use AC::Yenta::Kibitz::Status::Client;
13 use AC::Yenta::Debug 'kibitz_status';
14 use AC::Yenta::Config;
15 use AC::Yenta::Stats;
16 use AC::Yenta::MySelf;
17 use AC::Misc;
18  
19 use Sys::Hostname;
20 use Socket;
21 require 'AC/protobuf/yenta_status.pl';
22 use strict;
23  
24  
25 my $HOSTNAME = hostname();
26 my $ipinfo;
27  
28 ################################################################
29  
30 sub _myself {
31  
32 my $maps = conf_value('map');
33  
34 unless( $ipinfo ){
35 my $natinfo = my_network_info();
36 for my $i ( @$natinfo ){
37 push @$ipinfo, { ipv4 => inet_atoi($i->{ipa}), port => AC::Yenta::Status->my_port(), natdom => $i->{natdom} };
38 }
39 }
40  
41 return {
42 hostname => $HOSTNAME,
43 datacenter => my_datacenter(),
44 subsystem => 'yenta',
45 environment => conf_value('environment'),
46 via => AC::Yenta::Status->my_server_id(),
47 server_id => AC::Yenta::Status->my_server_id(),
48 instance_id => AC::Yenta::Status->my_instance_id(),
49 path => '.',
50 status => 200,
51 uptodate => AC::Yenta::Store::AE->up_to_date(),
52 timestamp => $^T,
53 lastup => $^T,
54 ip => $ipinfo,
55 map => [ keys %$maps ],
56 sort_metric => loadave() * 1000,
57 };
58 }
59  
60  
61 ################################################################
62  
63 sub myself {
64  
65 # tell server about ourself
66 return ACPYentaStatusRequest->encode({
67 myself => _myself(),
68 });
69 }
70  
71 sub response {
72  
73 # send client everything we know
74 my @peer = AC::Yenta::Status->allpeers();
75 push @peer, _myself();
76 # add the items we monitor
77 push @peer, AC::Yenta::Monitor::export();
78  
79 return ACPYentaStatusReply->encode({
80 status => \@peer,
81 });
82 }
83  
84 ################################################################
85  
86 # do not believe a client that says it is up
87 # put it on the sceptical queue, and check for ourself
88 sub update_sceptical {
89 my $gpb = shift;
90  
91 return unless $gpb;
92 my $c;
93 eval {
94 $c = ACPYentaStatusRequest->decode( $gpb );
95 $c = $c->{myself};
96 };
97 if(my $e = $@){
98 problem("cannot decode status data: $e");
99 return;
100 }
101  
102 my $id = $c->{server_id};
103  
104 # don't track myself
105 return if AC::Yenta::Status->my_server_id() eq $id;
106  
107 AC::Yenta::Status->update_sceptical($id, $c);
108 }
109  
110 sub update {
111 my $gpb = shift;
112  
113 return unless $gpb;
114 my $c;
115 eval {
116 $c = ACPYentaStatusReply->decode( $gpb );
117 };
118 if(my $e = $@){
119 problem("cannot decode status data: $e");
120 return;
121 }
122  
123 debug("rcvd update");
124 my $myself = AC::Yenta::Status->my_server_id();
125  
126 for my $up (@{$c->{status}}){
127 my $id = $up->{server_id};
128 next if $up->{via} eq $myself;
129 next if $id eq $myself;
130 AC::Yenta::Status->update($id, $up);
131 }
132 }
133  
134 # unable to connect to server. mark it down
135 sub isdown {
136 my $id = shift;
137  
138 return unless $id;
139  
140 AC::Yenta::Status->isdown( $id );
141 }
142 1;