Permalink
Browse files

second commit

  • Loading branch information...
1 parent 97d471e commit 38e6ce87565dde9c2ea3de75970dfe8c3b0d3285 @chenryn committed Nov 28, 2011
View
@@ -1 +1,8 @@
-This is my operation and maintenance tools.
+Here are my operation and maintenance tools. Right now, here including some tools as follows:
+a) An awk script using to compute the access bandwidth from apache-like log;
+b) An awk script using to compute the network bandwidth on the BSD system;
+c) A perl module using to manage linux cluster by ssh/expect/fork;
+d) Some scripts wrote for check_nrpe.
+e) A shell cgi demo using to distribute squid's configurations for customers. Wrote with sshpass and sed.
+f) A dynamic dns demo wrote by perl and its Stanford::DNSserver module, the demo's intention is to support response DNS query by views/weights/health_check/autoload and so on.
+g) A web accelerate effect monitor demo wrote by perl and funsioncharts.
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -w
+#my $ip = inet_aton("$ARGV[0]");
+my $ip = inet_aton(get_test_ip());
+my $file = $ARGV[1] || 'iplist.txt';
+my $length = `cat $file | wc -l`;
+
+my $code = get_area_code('quhao.txt');
+my @isplist = qw(other ctc cnc edu mobile);
+
+open my $fh, '<', "$file" or die "Cannot open $file\n";
+my $line_len = '26'; #=10+10+4+1+1£¬°üµ·ûâƪʳö˺ÿ´¶à¿ո񣬿ÉÔ¾µômy $first = 0;
+my $last = $length - 1; #ͳһʹÓSEEK_SET,ËÒ׺óеÄð»ÖÊlength-1
+my $result = 1;
+while ($result) {
+ my $middle = sprintf("%.0f",($last-$first) / 2 + $first); #Õ°ëÖ£¬³ýûÓsprintf±È±½Ónt¾«ȷ
+ seek $fh, $line_len * $middle, 0; #Ò¶¯µ½Õ°ëÖ
+ sysread $fh, $begin_ip, 10; #´Ó۰봦¶Á¡10λ
+ sysread $fh, $end_ip, 10; #½ÓÅٶÁ0λ,ȹû¿ոñҪÏseekÒ¶¯1λ,·³
+ #¸ù´óö´Îòöòë if ( $ip < $begin_ip ) {
+ $last = $middle;
+ next;
+ } elsif ( $ip > $end_ip ) {
+ $first = $middle;
+ next;
+ } else {
+ #Õµ½ÏӦǼ䣬¶Á¡ÇºźÍ˪ɺŠsysread $fh, $area, 4;
+ sysread $fh, $isp, 1;
+ printf "%010s %s %s\n", $ip, $code->{"$area"}, $isplist[$isp];
+ $result = 0; #ɶ¨$resultΪ¼٣¬Í³ö·
+ };
+};
+
+close $fh;
+
+sub inet_aton {
+ my $ip = shift;
+ my $short = sprintf "%010s", $1 * 256**3 + $2 * 256**2 + $3 * 256 + $4 if $ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
+ return $short;
+};
+#¶Ô¦ÇºźÍ¡·ݣ¬¸úµÄvÏ·´
+sub get_area_code {
+ my $file = shift;
+ my $area_code = { '0000' => 'other' };
+ open my $fh,'<',"$file" or die "Cannot open $file";
+ while (<$fh>) {
+ chomp;
+ my($area,$code) = split;
+ $area_code->{"$code"} = "$area";
+ }
+ close $fh;
+ return $area_code;
+};
+#ɳɻ¸öú·¨ipµØ·
+sub get_test_ip {
+ return join '.', map int rand 256, 1..4;
+}
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -w
+my($quhao, $qqwry) = @ARGV;
+$code = read_area_code($quhao);
+overwrite_iplist($qqwry, $code);
+sub inet_aton {
+ my $ip = shift;
+ my $short = sprintf "%010s", $1 * 256**3 + $2 * 256**2 + $3 * 256 + $4 if $ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
+ return $short;
+};
+
+sub read_area_code {
+ my $file = shift;
+ my $area_code = {};
+ open my $fh,'<',"$file" or die "Cannot open $file";
+ while (<$fh>) {
+ chomp;
+ my($area,$code) = split;
+ $area_code->{"$area"} = "$code";
+ }
+ close $fh;
+ return $area_code;
+};
+
+sub overwrite_iplist {
+ my($iplist, $area_code) = @_;
+ my($last_begin_ip_n, $last_end_ip_n, $last_province_n, $last_isp_n);
+ open my $fh,'<',"$iplist" or die "Cannoet open $iplist";
+ while (<$fh>) {
+ chomp;
+ my($begin_ip, $end_ip, $area, $isp) = split;
+ my($province_n, $isp_n);
+ my $begin_ip_n = &inet_aton("$begin_ip");
+ my $end_ip_n = &inet_aton("$end_ip");
+ next if ($end_ip_n - $begin_ip_n) < 32;
+ if ( $area =~ /ѧ/ ) {
+ $isp_n = 4; #½Ìýò廪±±´ó§У¼Ç¼ÔareaÀÁ£¬ËÒÕ²½Ìǰɶ¨
+ };
+ if ( $isp =~ m/µç/ ) {
+ $isp_n = 1; #µç
+ } elsif ( $isp =~ m// ) {
+ $isp_n = 2; #jͨ(°üø
+ } elsif ( $isp =~ m/Ìͨ|Ò¶¯/ ) {
+ $isp_n = 3; #Ò¶¯(°üú
+ } elsif ( $isp =~ m/ѧ/ ) {
+ $isp_n = 4; #½Ìý} else {
+ $isp_n = 0; #¹úַ¼°ÆËδÄʶ±ðúӪÉ
+ };
+
+ my $province = substr($area, 0, 4); #ÖÎÓ2×½ڣ¬ËÒ¶Ô­ʼ¼Ç¼»ñ°Ë×½ڼ´Ϊʡ·Ýû ( exists $area_code->{"$province"} ) {
+ $province_n = $area_code->{"$province"}; #¹ú֪µ绰ǺŵÄ¡·Ý
+ } else {
+ $province_n = '0000'; #¸۰Ĩ¼°Í¹úÄÓÆËδÄʶ±ðúַ
+ };
+ #϶Ϊºϲ¢Í¶Σ¬֮ǰdnsʱҲÓ¹ý(!$last_province_n) {
+ ($last_begin_ip_n, $last_end_ip_n, $last_province_n, $last_isp_n) = ($begin_ip_n, $end_ip_n, $province_n, $isp_n);
+ };
+ if ( $last_province_n == $province_n && $last_isp_n == $isp_n ) {
+ $last_end_ip_n = $end_ip_n;
+ } else {
+ printf "%010s %010s %04s %s\n", $last_begin_ip, $last_end_ip, $last_province_n, $last_isp_n;
+ ($last_begin_ip_n, $last_end_ip_n, $last_province_n, $last_isp_n) = ($begin_ip_n, $end_ip_n, $province_n, $isp_n);
+ };
+ };
+ close $fh;
+};
@@ -0,0 +1,107 @@
+use Dancer ':syntax';
+use Dancer::Plugin::Database;
+use POSIX qw(strftime);
+
+get '/xml' => sub {
+ my $begin_time = date_format(params->{begin});
+ my $end_time = date_format(params->{end});
+ my $type = params->{type};
+ my $color = { chinacache => '1D8BD1',
+ dnion => 'F1683C',
+ fastweb => '2AD62A',
+ };
+ my $xml_head = "<graph caption='Response Time' subcaption='from $begin_time to $end_time' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='3' numVdivlines='0' yaxisminvalue='1000' yaxismaxvalue='1800' rotateNames='1'>\n<categories >\n";
+ my $group;
+ if ( $type eq 'time' ) {
+ $group = 'cur_date';
+ } elsif ( $type eq 'isp' ) {
+ $group = 'isp';
+ } elsif ( $type eq 'area' ) {
+ $group = 'area';
+ } else {
+ return 'Error';
+ };
+ my $xml = cdn_select($begin_time, $end_time, $group, $color, $xml_head);
+ return $xml;
+};
+
+sub get_area_code {
+ my $file = shift;
+ my $area_code = { '0000' => 'ÆË' };
+ open my $fh,'<',"$file" or die "Cannot open $file";
+ while (<$fh>) {
+ chomp;
+ my($area,$code) = split;
+ $area_code->{"$code"} = "$area";
+ }
+ close $fh;
+ return $area_code;
+};
+
+sub cdn_select {
+ my ($begin_time, $end_time, $group, $color, $xml) = @_;
+ my $sql = "SELECT ${group},AVG(avg_time) avg FROM cdn_cron_record WHERE cdn = ? AND cur_date BETWEEN ? AND ? GROUP BY ${group} ORDER BY ${group}";
+ my $sth = database->prepare($sql);
+ my $i = 0;
+ for my $cdn (qw{chinacache dnion fastweb}) {
+ $sth->execute($cdn, $begin_time, $end_time);
+ unless($i) {
+ my @values;
+ while ( my $ref = $sth->fetchrow_hashref ) {
+ my ($avg_time, $type) = ($ref->{'avg'}, $ref->{"$group"});
+ $xml .= "<category name='convert_group($group, $type)' />\n";
+ push @values, $avg_time;
+ };
+ $xml .= "</category>\n";
+ $xml .= "<dataset seriesName='$cdn' color='$color->{$cdn}'>\n";
+ $xml .= "<set value='$_' />\n" for @values;
+ $xml .= "</dataset>\n";
+ } else {
+ $xml .= "<dataset seriesName='$cdn' color='$color->{$cdn}'>\n";
+ while ( my $ref = $sth->fetchrow_hashref ) {
+ $xml .= "<set value='$ref->{avg}' />\n";
+ };
+ $xml .= "</dataset>\n";
+ };
+ $i++;
+ };
+ $xml .= '</graph>';
+ return $xml;
+};
+
+sub convert_group {
+ my ($group, $origin) = @_;
+ if ($group eq 'cur_date') {
+ return $origin;
+ } elsif ($group eq 'isp') {
+ my @isplist = qw(other ctc cnc mobile edu);
+ return $isplist[$origin];
+ } elsif ($group eq 'area') {
+ my $arealist = get_area_code('quhao.txt');
+ my $code = sprintf("%04s",$origin);
+ return $arealist->{$code};
+ } else {
+ return 'Error';
+ };
+};
+
+sub date_format {
+ my $time = shift;
+ return strftime("%F %H:%M",localtime($time)) if $time =~ m/\d+/;
+};
+use Time::Local;
+get '/cdncharts' => sub {
+ my $type = params->{chartstype};
+ my $begin = unix_time_format(params->{timefrom});
+ my $end = unix_time_format(params->{timeto});
+ my $req_url = "/xml?begin=${begin}&end=${end}&type=${type}";
+ my $line = 1 if $type eq 'time';
+ template 'charts', { line => $line, url => "$req_url", };
+};
+
+sub unix_time_format {
+ my $time = shift;
+ if ( $time =~ m/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/ ) {
+ return timelocal('00',$5,$4,$3,$2-1,$1-1900);
+ };
+};
@@ -0,0 +1,10 @@
+<HTML>
+<HEAD>
+<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
+<TITLE>CDN Monitor</TITLE>
+</HEAD>
+<FRAMESET BORDER="0" FRAMEBORDER="0" FRAMESPACING="0" COLS="270,*">
+<FRAME SRC="side.html" NAME="side" TARGET="main">
+<FRAME SRC="main.html" NAME="main">
+</FRAMESET>
+</HTML>
@@ -0,0 +1,15 @@
+<link type="text/css" rel="stylesheet" href="css/calendar.css" />
+<script language="javascript" src="javascripts/calendar.js"></script>
+select_time<HR>
+<form action="/cdncharts" method="get" target="main">
+<LI>begin</LI>
+<input name="timefrom" type="text" id="timefrom" style="width:100%;" onclick="displayCalendar(this, 'yyyy-mm-dd hh:ii', this, true, '');"/>
+<LI>end</LI>
+<input name="timeto" type="text" id="timeto" style="width:100%;" onclick="displayCalendar(this, 'yyyy-mm-dd hh:ii', this, true, '');"/><HR>
+<select name="chartstype" id="chartstype">
+<option value="area">area</option>
+<option value="isp">isp</option>
+<option value="time" selected>time</option>
+</select>
+<input type="submit" value="submit" id="submit">
+</form>
@@ -0,0 +1,21 @@
+<html>
+<head>
+<title>FusionCharts Free Documentation</title>
+<link rel="stylesheet" href="css/chartstyle.css" type="text/css" />
+<script language="JavaScript" src="javascripts/FusionCharts.js"></script>
+</head>
+
+<body>
+<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
+ <tr>
+ <td valign="top" class="text" align="center"> <div id="chartdiv" align="center">
+ FusionCharts. </div>
+ <script type="text/javascript">
+ var chart = new FusionCharts("[% IF line %]chartswf/FCF_MSLine.swf[% ELSE %]chartswf/FCF_MSBar2D.swf[% END %]", "ChartId", "400", "350");
+ chart.setDataURL(escape("[% url %]"));
+ chart.render("chartdiv");
+ </script> </td>
+ </tr>
+</table>
+</body>
+</html>
@@ -0,0 +1,9 @@
+ctc_hebei:
+ ip:
+ - 10.168.168.1
+ - 10.168.169.2
+ - 10.168.170.3
+ per:
+ - 50
+ - 30
+ - 20
Oops, something went wrong.

0 comments on commit 38e6ce8

Please sign in to comment.