| 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$ | ||
| 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 | |||
| 27 | ################################################################ | ||
| 28 | |||
| 29 | sub _myself { | ||
| 30 | |||
| 31 | my $maps = conf_value('map'); | ||
| 32 | |||
| 33 | my @ipinfo; | ||
| 34 | my $natinfo = my_network_info(); | ||
| 35 | my $status = 200; | ||
| 36 | |||
| 37 | for my $i ( @$natinfo ){ | ||
| 38 | my $st = AC::Yenta::NetMon::status_dom( $i->{natdom} ); | ||
| 39 | |||
| 40 | # if a private network is down, announce the network + 500 | ||
| 41 | # if a private network is down, stop announcing it | ||
| 42 | |||
| 43 | if( $i->{natdom} ){ | ||
| 44 | push @ipinfo, { ipv4 => inet_atoi($i->{ipa}), port => AC::Yenta::Status->my_port(), natdom => $i->{natdom} } | ||
| 45 | if $st == 200; | ||
| 46 | }else{ | ||
| 47 | push @ipinfo, { ipv4 => inet_atoi($i->{ipa}), port => AC::Yenta::Status->my_port(), natdom => $i->{natdom} }; | ||
| 48 | $status = 500 unless $st == 200; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return { | ||
| 53 | hostname => $HOSTNAME, | ||
| 54 | datacenter => my_datacenter(), | ||
| 55 | subsystem => 'yenta', | ||
| 56 | environment => conf_value('environment'), | ||
| 57 | via => AC::Yenta::Status->my_server_id(), | ||
| 58 | server_id => AC::Yenta::Status->my_server_id(), | ||
| 59 | instance_id => AC::Yenta::Status->my_instance_id(), | ||
| 60 | path => '.', | ||
| 61 | status => $status, | ||
| 62 | uptodate => AC::Yenta::Store::AE->up_to_date(), | ||
| 63 | timestamp => $^T, | ||
| 64 | lastup => $^T, | ||
| 65 | ip => \@ipinfo, | ||
| 66 | map => [ keys %$maps ], | ||
| 67 | sort_metric => loadave() * 1000, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | ################################################################ | ||
| 73 | |||
| 74 | sub myself { | ||
| 75 | |||
| 76 | # tell server about ourself | ||
| 77 | return ACPYentaStatusRequest->encode({ | ||
| 78 | myself => _myself(), | ||
| 79 | }); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub response { | ||
| 83 | |||
| 84 | # send client everything we know | ||
| 85 | my @peer = AC::Yenta::Status->allpeers(); | ||
| 86 | push @peer, _myself(); | ||
| 87 | # add the items we monitor | ||
| 88 | push @peer, AC::Yenta::Monitor::export(); | ||
| 89 | |||
| 90 | return ACPYentaStatusReply->encode({ | ||
| 91 | status => \@peer, | ||
| 92 | }); | ||
| 93 | } | ||
| 94 | |||
| 95 | ################################################################ | ||
| 96 | |||
| 97 | # do not believe a client that says it is up | ||
| 98 | # put it on the sceptical queue, and check for ourself | ||
| 99 | sub update_sceptical { | ||
| 100 | my $gpb = shift; | ||
| 101 | my $io = shift; | ||
| 102 | |||
| 103 | return unless $gpb; | ||
| 104 | my $c; | ||
| 105 | eval { | ||
| 106 | $c = ACPYentaStatusRequest->decode( $gpb ); | ||
| 107 | $c = $c->{myself}; | ||
| 108 | }; | ||
| 109 | if(my $e = $@){ | ||
| 110 | problem("cannot decode status data: $e"); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $id = $c->{server_id}; | ||
| 115 | |||
| 116 | # don't track myself | ||
| 117 | return if AC::Yenta::Status->my_server_id() eq $id; | ||
| 118 | |||
| 119 | AC::Yenta::Status->update_sceptical($id, $c, $io); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub update { | ||
| 123 | my $gpb = shift; | ||
| 124 | |||
| 125 | return unless $gpb; | ||
| 126 | my $c; | ||
| 127 | eval { | ||
| 128 | $c = ACPYentaStatusReply->decode( $gpb ); | ||
| 129 | }; | ||
| 130 | if(my $e = $@){ | ||
| 131 | problem("cannot decode status data: $e"); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | debug("rcvd update"); | ||
| 136 | my $myself = AC::Yenta::Status->my_server_id(); | ||
| 137 | |||
| 138 | for my $up (@{$c->{status}}){ | ||
| 139 | my $id = $up->{server_id}; | ||
| 140 | next if $up->{via} eq $myself; | ||
| 141 | next if $id eq $myself; | ||
| 142 | AC::Yenta::Status->update($id, $up); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # unable to connect to server. mark it down | ||
| 147 | sub isdown { | ||
| 148 | my $id = shift; | ||
| 149 | |||
| 150 | return unless $id; | ||
| 151 | |||
| 152 | AC::Yenta::Status->isdown( $id ); | ||
| 153 | } | ||
| 154 | 1; |