Merge branch 'master' of github.com:dpavlin/angular-mojolicious
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 6 Dec 2010 22:34:55 +0000 (23:34 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 6 Dec 2010 22:34:55 +0000 (23:34 +0100)
.gitmodules
common/snmp-json [new submodule]
couchdb-trigger.pl
public/app/drzb2011/replication-pull.sh [new file with mode: 0755]
scripts/snmp-printer.pl [deleted file]
trigger/email.pm [new file with mode: 0644]
trigger/shell.pm [new file with mode: 0644]

index 1912998..c8c5fc5 100644 (file)
@@ -4,3 +4,6 @@
 [submodule "common/mojo"]
        path = common/mojo
        url = http://github.com/kraih/mojo.git
+[submodule "common/snmp-json"]
+       path = common/snmp-json
+       url = git://github.com/dpavlin/snmp-json.git
diff --git a/common/snmp-json b/common/snmp-json
new file mode 160000 (submodule)
index 0000000..bfa80c3
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit bfa80c3c2349d71aeb4c84eb3b3e16fcdb87f12e
index d39f007..02f5c32 100755 (executable)
@@ -18,21 +18,14 @@ use lib 'common/mojo/lib';
 use Mojo::Client;
 use Mojo::JSON;
 use Time::HiRes qw(time);
+use Data::Dump qw(dump);
 
-my $url = 'http://localhost:5984/monitor';
+my ( $url, $trigger_path ) = @ARGV;
 
-sub _trigger {
-       my $trigger = $_[0]->{trigger};
-       if ( my $command = $trigger->{command} ) {
-               # FIXME SECURITY HOLE
-               my $output = $trigger->{output} = `$command`;
+$url          ||= 'http://localhost:5984/monitor';
+$trigger_path ||= 'trigger/shell.pm' ;
 
-               $trigger->{output} =
-                       [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ]
-                       if $trigger->{format} =~ m/table/i;
-       }
-       return $trigger;
-}
+require $trigger_path if -e $trigger_path;
 
 my $seq = 0;
 
@@ -77,11 +70,11 @@ while( ! $error ) {
 
                                debug 'change' => $change;
 
-                               if ( my $trigger = $change->{doc}->{trigger} ) {
-                                       if ( exists $trigger->{active} ) {
+                               if ( filter($change) ) {
+                                       if ( exists $change->{doc}->{trigger}->{active} ) {
                                                debug 'trigger.active',  $change->{doc}->{trigger}->{active};
                                        } else {
-                                               $trigger->{active} = [ time() ];
+                                               $change->{doc}->{trigger}->{active} = [ time() ];
 
                                                debug 'TRIGGER start PUT ', $change->{doc};
                                                $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
@@ -97,9 +90,9 @@ while( ! $error ) {
                                                                $change->{doc}->{_rev} = $res->{rev};
 
                                                                debug "TRIGGER execute ", $change->{doc};
-                                                               _trigger( $change->{doc} );
+                                                               trigger( $change );
 
-                                                               push @{ $trigger->{active} }, time(), 0; # last timestamp
+                                                               push @{ $change->{doc}->{trigger}->{active} }, time(), 0; # last timestamp
 
                                                                $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
                                                                        my ($client,$tx) = @_;
diff --git a/public/app/drzb2011/replication-pull.sh b/public/app/drzb2011/replication-pull.sh
new file mode 100755 (executable)
index 0000000..12c8eb4
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/sh -x
+
+curl -v -X POST localhost:5984/_replicate -d '{"source":"http://10.60.0.95:5984/drzb2011","target":"drzb2011"}'
diff --git a/scripts/snmp-printer.pl b/scripts/snmp-printer.pl
deleted file mode 100755 (executable)
index e9553c5..0000000
+++ /dev/null
@@ -1,161 +0,0 @@
-#!/usr/bin/perl
-use warnings;
-use strict;
-
-# pull info from SNMP enabled printers and dump JSON
-#
-# dump all snmp data on printer with:
-# snmpwalk -c public -v 1 10.60.3.15
-
-use SNMP::Multi;
-use Data::Dump qw(dump);
-
-my $dir = 'public/json/monitor/printers';
-die "output directory $dir: $!" unless -d $dir;
-
-use JSON;
-sub save_json {
-       my ( $ip, $json ) = @_;
-       my $path = "$dir/$ip";
-       open(my $fh, '>', $path) || die "$path: $!";
-       print $fh encode_json $json;
-       close($fh);
-       warn "# $path ", -s $path, " bytes\n";
-}
-
-sub iso_datetime {
-       my ($ss,$mm,$hh,$d,$m,$y) = localtime(time);
-       return sprintf "%04d-%02d-%02dT%02d:%02d:%02d", $y+1900, $m, $d, $hh, $mm, $ss;
-}
-
-my $log_path = join('.', $dir, (split(/T/,iso_datetime,2))[0], 'json');
-open(my $log, '>>', $log_path) || die "$log_path: $!";
-
-my $debug = $ENV{DEBUG} || 0; 
-
-my $community = 'public';
-my @printers = qw(
-10.60.0.20
-
-10.60.3.15
-10.60.3.17
-
-10.60.3.19
-10.60.3.21
-
-10.60.3.23
-10.60.3.25
-
-10.60.3.27
-10.60.3.29
-
-10.60.3.31
-10.60.3.33
-
-10.60.3.35
-10.60.3.37
-);
-
-@printers = @ARGV if @ARGV;
-
-# remove final .1 since we are using bulkwalk to get values!
-my %vars = qw[
-info                           iso.3.6.1.2.1.1.1.0
-hostname                       iso.3.6.1.2.1.43.5.1.1.16.1
-serial                         iso.3.6.1.2.1.43.5.1.1.17.1
-pages                          iso.3.6.1.2.1.43.10.2.1.4.1
-@message                       iso.3.6.1.2.1.43.18.1.1.8
-@consumable_name       iso.3.6.1.2.1.43.11.1.1.6.1
-@consumable_max                iso.3.6.1.2.1.43.11.1.1.8.1
-@consumable_curr       iso.3.6.1.2.1.43.11.1.1.9.1
-@tray_max                      iso.3.6.1.2.1.43.8.2.1.9.1
-@tray_capacity         iso.3.6.1.2.1.43.8.2.1.10.1
-@tray_name                     iso.3.6.1.2.1.43.8.2.1.13.1
-@tray_dim_x                    iso.3.6.1.2.1.43.8.2.1.4.1
-@tray_dim_y                    iso.3.6.1.2.1.43.8.2.1.5.1
-];
-
-my $oid2name;
-my @vars;
-while ( my ($name,$oid) = each %vars ) {
-       $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
-       push @vars, [ $oid ];
-       $oid2name->{$oid} = $name;
-}
-my @oids = sort { length $a <=> length $b } keys %$oid2name;
-warn "# vars = ",dump(@vars) if $debug;
-
-my $sm = SNMP::Multi->new(
-       Method    => 'bulkwalk',
-       Community => $community,
-       Requests  => SNMP::Multi::VarReq->new(
-               hosts => [ @printers ],
-               vars  => [ @vars ],
-    ),
-       Timeout     => 1,
-       Retries     => 0,
-) or die $SNMP::Multi::error;
-
-warn "# working on: ", join(' ', @printers),$/;
-
-my $resp = $sm->execute() or die $sm->error();
-
-my $collected;
-
-foreach my $host ( $resp->hosts ) {
-       my $status;
-
-       foreach my $result ( $host->results ) {
-               if ( $result->error ) {
-                       warn "ERROR: $host ", $result->error;
-                       next;
-               }
-
-               warn "## result = ", dump($result) if $debug;
-
-               foreach my $v ( $result->varlists ) {
-                       foreach my $i ( @$v ) {
-                               my ( $oid, undef, $val, $fmt ) = @$i;
-                               if ( my $name = $oid2name->{$oid} ) {
-                                       $status->{$name} = $val;
-                               } else {
-                                       my $oid_base;
-                                       foreach ( @oids ) {
-                                               my $oid_part = substr($oid,0,length($_));
-                                               if ( $oid_part eq $_ ) {
-                                                       $oid_base = $oid_part;
-                                                       last;
-                                               }
-                                       }
-
-                                       my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
-                                       if ( $name =~ s/^\@// ) {
-                                               push @{ $status->{$name} }, $val;
-                                       } else {
-                                               $status->{$name} = $val;
-                                       }
-                               }
-                       }
-               }
-
-       }
-
-       foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
-               my ( $prefix,$name ) = split(/_/,$group,2);
-               foreach my $i ( 0 .. $#{ $status->{$group} } ) {
-                       $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
-               }
-               delete $status->{$group};
-       }
-
-       $status->{ip} = $host->hostname;
-       $status->{date} = iso_datetime;
-       print "$host = ",dump($status);
-       save_json $host => $status;
-       $collected->{$host} = $status;
-
-       print $log encode_json($status),"\n";
-}
-
-close($log);
-warn "# log $log_path ", -s $log_path, " bytes\n";
diff --git a/trigger/email.pm b/trigger/email.pm
new file mode 100644 (file)
index 0000000..e9739f9
--- /dev/null
@@ -0,0 +1,12 @@
+sub filter {
+       my $change = shift;
+       return 1 if $change->{doc}->{person}->{email};
+}
+
+sub trigger {
+       my $change = shift;
+       warn "# send_email ",dump($change->{doc}->{person});
+       $change->{doc}->{email_sent}++;
+}
+
+1;
diff --git a/trigger/shell.pm b/trigger/shell.pm
new file mode 100644 (file)
index 0000000..1c89f8b
--- /dev/null
@@ -0,0 +1,20 @@
+sub filter {
+       my $change = shift;
+       return 1 if $change->{doc}->{trigger}->{command};
+}
+
+
+sub trigger {
+       my $change = shift;
+       my $trigger = $change->{doc}->{trigger};
+       if ( my $command = $trigger->{command} ) {
+               # FIXME SECURITY HOLE
+               my $output = $trigger->{output} = `$command`;
+
+               $trigger->{output} =
+                       [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ]
+                       if $trigger->{format} =~ m/table/i;
+       }
+}
+
+1;