| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Apr-06 13:27 (EDT) | ||
| 6 | # Function: internal stats monitoring | ||
| 7 | # | ||
| 8 | # $Id$ | ||
| 9 | |||
| 10 | package AC::Yenta::Stats; | ||
| 11 | use AC::Yenta::Debug 'stats'; | ||
| 12 | use AC::Import; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | our @EXPORT = qw(add_idle loadave inc_stat); | ||
| 16 | require AC::Yenta::Store; | ||
| 17 | AC::Yenta::Store->import(); | ||
| 18 | |||
| 19 | my $loadave = 0; | ||
| 20 | my %STATS; | ||
| 21 | |||
| 22 | my %HANDLER = ( | ||
| 23 | loadave => \&http_load, | ||
| 24 | stats => \&http_stats, | ||
| 25 | status => \&http_status, | ||
| 26 | peers => \&AC::Yenta::Status::report, | ||
| 27 | dumppeers => \&AC::Yenta::Status::report_long, | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub add_idle { | ||
| 31 | my $idle = shift; | ||
| 32 | my $total = shift; | ||
| 33 | |||
| 34 | # decaying average | ||
| 35 | return unless $total; | ||
| 36 | my $load = 1 - $idle / $total; | ||
| 37 | $total = 60 if $total > 60; | ||
| 38 | my $exp = exp( - $total / 60 ); | ||
| 39 | $loadave = $loadave * $exp + $load * (1 - $exp); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub loadave { | ||
| 43 | return $loadave; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub inc_stat { | ||
| 47 | my $stat = shift; | ||
| 48 | |||
| 49 | $STATS{$stat} ++; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub handler { | ||
| 53 | my $class = shift; | ||
| 54 | my $io = shift; | ||
| 55 | my $proto = shift; | ||
| 56 | my $url = shift; | ||
| 57 | |||
| 58 | debug("http request $url"); | ||
| 59 | $url =~ s|^/||; | ||
| 60 | $url =~ s/%(..)/chr(hex($1))/eg; | ||
| 61 | |||
| 62 | my $f = $HANDLER{$url}; | ||
| 63 | $f = \&http_data if $url =~ m|^data/|; | ||
| 64 | $f = \&http_file if $url =~ m|^file/|; | ||
| 65 | $f ||= \&http_notfound; | ||
| 66 | my( $content, $code, $text ) = $f->($url); | ||
| 67 | $code ||= 200; | ||
| 68 | $text ||= 'OK'; | ||
| 69 | |||
| 70 | my $res = "HTTP/1.0 $code $text\r\n" | ||
| 71 | . "Server: AC/Yenta\r\n" | ||
| 72 | . "Connection: close\r\n" | ||
| 73 | . "Content-Type: text/plain; charset=UTF-8\r\n" | ||
| 74 | . "Content-Length: " . length($content) . "\r\n" | ||
| 75 | . "\r\n" | ||
| 76 | . $content ; | ||
| 77 | |||
| 78 | $io->write($res); | ||
| 79 | $io->set_callback('write_buffer_empty', \&_done ); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _done { | ||
| 83 | my $io = shift; | ||
| 84 | $io->shut(); | ||
| 85 | } | ||
| 86 | |||
| 87 | ################################################################ | ||
| 88 | |||
| 89 | sub http_notfound { | ||
| 90 | my $url = shift; | ||
| 91 | |||
| 92 | return ("404 NOT FOUND\nThe requested url /$url was not found on this server.\nSo sorry.\n\n", 404, "Not Found"); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub http_load { | ||
| 96 | |||
| 97 | return sprintf("loadave: %0.4f\n\n", loadave()); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub http_status { | ||
| 101 | my $status = AC::Yenta::NetMon::status_dom('public'); | ||
| 102 | return "status: OK\n\n" if $status == 200; | ||
| 103 | return("status: PROBLEM\n\n", 500, "Problem"); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub http_stats { | ||
| 107 | |||
| 108 | my $res; | ||
| 109 | for my $k (sort keys %STATS){ | ||
| 110 | $res .= sprintf("%-24s%s\n", "$k:", $STATS{$k}); | ||
| 111 | } | ||
| 112 | |||
| 113 | my @peers = AC::Yenta::Status->allpeers(); | ||
| 114 | $res .= sprintf("%-24s%s\n", "peers:", scalar @peers); | ||
| 115 | $res .= "\n"; | ||
| 116 | return $res; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub http_data { | ||
| 120 | my $url = shift; | ||
| 121 | |||
| 122 | my(undef, $map, $key, $ver) = split m|/|, $url; | ||
| 123 | my($data, $version, $file, $meta) = store_get($map, $key, $ver); | ||
| 124 | |||
| 125 | return http_notfound($url) unless $version; | ||
| 126 | return $data; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub http_file { | ||
| 130 | my $url = shift; | ||
| 131 | |||
| 132 | my(undef, $map, $key, $ver) = split m|/|, $url; | ||
| 133 | my($data, $version, $file, $meta) = store_get($map, $key, $ver); | ||
| 134 | |||
| 135 | return http_notfound($url) unless $version && $file; | ||
| 136 | return $$file; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; |