From: cecilihf Date: Mon, 16 Jul 2007 08:39:50 +0000 (+0000) Subject: Webmin plugin for varnish X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4e23c78ff38bd873975d84fba75187053a47b06;p=varnish Webmin plugin for varnish git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1706 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-tools/webmin/Varnish/CLI.pm b/varnish-tools/webmin/Varnish/CLI.pm new file mode 100755 index 00000000..c4930df9 --- /dev/null +++ b/varnish-tools/webmin/Varnish/CLI.pm @@ -0,0 +1,60 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +package Varnish::CLI; + +use IO::Socket; + +# Send a command to varnish over telnet. +# This function takes a hash as parameter. +# Valid keys for the hash are: +# * address : Address of varnish +# * port: port where varnish listens for telnet connections +# * command : the command to send +# * params : parameters to the command as a string. (optional) +# The function returns a tuple with the return code and the result. +# If missing or invalid parameters, 0 is returned. +sub send_command { + my ($args) = shift; + my $sock; + my $line; + my $status, $size, $read; + my $response = ""; + + if (!$args->{'address'} || !$args->{'port'} || !$args->{'command'}) { + return 0; + } + + $sock = IO::Socket::INET->new( + Proto => "tcp", + PeerAddr => $args->{'address'}, + PeerPort => $args->{'port'}, + ) or return 0; + + if ($args->{'params'}) { + $args->{'command'} .= " $args->{'params'}"; + } + print $sock "$args->{'command'}\r\n"; + + if (!($line = <$sock>)) { + return 0; + } + $line =~ /^(\d+) (\d+)/; + $status = $1; + $size = $2; + + while ($line = <$sock>) { + $response .= $line; + $read += length($line); + if ($read >= $size) { + last; + } + } + + close $sock; + + return ($status, $response); +} + +return 1; \ No newline at end of file diff --git a/varnish-tools/webmin/config.info b/varnish-tools/webmin/config.info new file mode 100644 index 00000000..905e8ffa --- /dev/null +++ b/varnish-tools/webmin/config.info @@ -0,0 +1,2 @@ +address=Address to varnish,3,localhost +port=Port number varnish listens for telnet connections on,3,23 diff --git a/varnish-tools/webmin/images/icon.gif b/varnish-tools/webmin/images/icon.gif new file mode 100644 index 00000000..fc5b8e9e Binary files /dev/null and b/varnish-tools/webmin/images/icon.gif differ diff --git a/varnish-tools/webmin/images/params_icon.gif b/varnish-tools/webmin/images/params_icon.gif new file mode 100755 index 00000000..495a4292 Binary files /dev/null and b/varnish-tools/webmin/images/params_icon.gif differ diff --git a/varnish-tools/webmin/images/purge_icon.gif b/varnish-tools/webmin/images/purge_icon.gif new file mode 100755 index 00000000..3da735b6 Binary files /dev/null and b/varnish-tools/webmin/images/purge_icon.gif differ diff --git a/varnish-tools/webmin/images/status_icon.gif b/varnish-tools/webmin/images/status_icon.gif new file mode 100755 index 00000000..ee21b68c Binary files /dev/null and b/varnish-tools/webmin/images/status_icon.gif differ diff --git a/varnish-tools/webmin/images/vcl_icon.gif b/varnish-tools/webmin/images/vcl_icon.gif new file mode 100755 index 00000000..ce1f3a3f Binary files /dev/null and b/varnish-tools/webmin/images/vcl_icon.gif differ diff --git a/varnish-tools/webmin/index.cgi b/varnish-tools/webmin/index.cgi new file mode 100644 index 00000000..e5186dc1 --- /dev/null +++ b/varnish-tools/webmin/index.cgi @@ -0,0 +1,13 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; + +&ui_print_header(undef, $module_info{'name'}, "", undef, 1, 1); + +@olinks = ("varnish_vcl.cgi", "varnish_params.cgi", "varnish_status.cgi", "varnish_purge.cgi"); +@otitles = ("VCL", "Params", "Start/Stop", "Purge"); +@oicons = ("images/vcl_icon.gif", "images/params_icon.gif", "images/status_icon.gif", "images/purge_icon.gif"); + +&icons_table(\@olinks, \@otitles, \@oicons); diff --git a/varnish-tools/webmin/module.info b/varnish-tools/webmin/module.info new file mode 100644 index 00000000..6bb49135 --- /dev/null +++ b/varnish-tools/webmin/module.info @@ -0,0 +1,3 @@ +name=Varnish Admin +desc=Varnish Admin + diff --git a/varnish-tools/webmin/varnish_params.cgi b/varnish-tools/webmin/varnish_params.cgi new file mode 100644 index 00000000..12e03041 --- /dev/null +++ b/varnish-tools/webmin/varnish_params.cgi @@ -0,0 +1,71 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ui_print_header(undef, "Params", "", undef, 1, 1); +&ReadParse(); +&error_setup("Failed"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'param.show'); +my ($status, $res) = Varnish::CLI::send_command(\%attrs); + +if (!($status eq '200')) { + &error("$status - $res"); +} + +my @params = (); +foreach my $line (split(/\n/, $res)) { + if ($line =~ /(\w+)\s+([a-zA-z0-9.:]+)(\s+(\[|\()(\w+|%)(\]|\)))?/) { + push @params, {'name' => $1, 'value' => $2, 'unit' => $5}; + } +} +if ($in{'save'}) { + foreach $param (@params) { + if ($in{$param->{'name'}} && !($in{$param->{'name'}} eq $param->{'value'})) { + $attrs{'command'} = 'param.set'; + $attrs{'params'} = "$param->{'name'} $in{$param->{'name'}}"; + $status, $res = Varnish::CLI::send_command(\%attrs); + if (!($status eq '200')) { + &error("$status - $res"); + } + $param->{'value'} = $in{$param->{'name'}}; + } + } +} + +my $cmd_num = 0; +print "
"; +print "\n"; +print "\n"; +print "
params
\n"; +foreach $param (@params) { + if (($cmd_num % 2) == 0) { + print "\n"; + } + print "\n"; + if (($cmd_num % 2) != 0) { + print "\n"; + } + $cmd_num++; +} +print "
$param->{'name'}"; + if ($param->{'unit'} eq 'bool') { + print "{'name'} value=on " . + (($param->{'value'} eq 'on') ? "checked" : "") . " /> On\n"; + print "{'name'} value=off " . + (($param->{'value'} eq 'off') ? "checked" : "") . " /> Off\n"; + + } + else { + print "{'name'} value=$param->{'value'} size=25 /> $param->{'unit'}"; + } + print "
"; +print "
"; +print ""; +print "
"; + +&ui_print_footer("", 'module index'); diff --git a/varnish-tools/webmin/varnish_purge.cgi b/varnish-tools/webmin/varnish_purge.cgi new file mode 100644 index 00000000..13b0c205 --- /dev/null +++ b/varnish-tools/webmin/varnish_purge.cgi @@ -0,0 +1,34 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ui_print_header(undef, "Purge", "", undef, 1, 1); + +&ReadParse(); +&error_setup("Purge failed"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'url.purge'); +if ($in{'regexp'}) { + $attrs{'params'} = $in{'regexp'}; + my ($status, $res) = Varnish::CLI::send_command(\%attrs); + + if ($status eq '200') { + print "Purging succesful
"; + print "$res
"; + print "
"; + } + else { + &error("$status - $res"); + } +} + +print "
\n"; +print "What to purge (regexp):
\n"; +print "
\n"; +print "
"; + +&ui_print_footer("", 'module index'); diff --git a/varnish-tools/webmin/varnish_start.cgi b/varnish-tools/webmin/varnish_start.cgi new file mode 100644 index 00000000..9c1abb7c --- /dev/null +++ b/varnish-tools/webmin/varnish_start.cgi @@ -0,0 +1,19 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ReadParse(); +&error_setup("Start command failed: "); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'start'); +my ($status, $res) = Varnish::CLI::send_command(\%attrs); +if (!($status eq 200)) { + &error("$status - $res"); +} +&webmin_log("start"); +&redirect("varnish_status.cgi"); + diff --git a/varnish-tools/webmin/varnish_status.cgi b/varnish-tools/webmin/varnish_status.cgi new file mode 100644 index 00000000..c8e114a3 --- /dev/null +++ b/varnish-tools/webmin/varnish_status.cgi @@ -0,0 +1,43 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ui_print_header(undef, "Status", "", undef, 1, 1); +&error_setup("Could not get status"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'ping'); +my ($status, $res) = Varnish::CLI::send_command(\%attrs); +if ($status eq '200') { + if ($res =~ /^PONG/) { + print "Varnish is up
"; + } + else { + print "Varnish is down
"; + } +} +else { + &error("$status - $res"); +} + +$attrs{'command'} = 'status'; +$status, $res = Varnish::CLI::send_command(\%attrs); +if ($status eq '200') { + print "$res
"; +} +else { + &error("$status - $res"); +} + +print "
\n"; +print &ui_buttons_start(); + +print &ui_buttons_row("varnish_stop.cgi","Stop varnish child"); +print &ui_buttons_row("varnish_start.cgi", "Start varnish child"); + +print &ui_buttons_end(); + +&ui_print_footer("", 'module index'); diff --git a/varnish-tools/webmin/varnish_stop.cgi b/varnish-tools/webmin/varnish_stop.cgi new file mode 100644 index 00000000..3565cb18 --- /dev/null +++ b/varnish-tools/webmin/varnish_stop.cgi @@ -0,0 +1,19 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ReadParse(); +&error_setup("Stop command failed: "); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'stop'); +my ($status, $res) = Varnish::CLI::send_command(\%attrs); +if (!($status eq 200)) { + &error("$status - $res"); +} +&webmin_log("stop"); +&redirect("varnish_status.cgi"); + diff --git a/varnish-tools/webmin/varnish_vcl.cgi b/varnish-tools/webmin/varnish_vcl.cgi new file mode 100644 index 00000000..283ce4a6 --- /dev/null +++ b/varnish-tools/webmin/varnish_vcl.cgi @@ -0,0 +1,60 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ui_print_header(undef, "VCL Admin", "", undef, 1, 1); +&error_setup("Failed"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'vcl.list'); +my ($status, $res) = Varnish::CLI::send_command(\%attrs); + +if (!($status eq '200')) { + &error("$status - $res"); +} + +print ""; +print "\n"; +print ""; +print "\n"; +print ""; +print "
Available vcl files
"; +print "
"; +my $confname = ""; +my $set = 0; +foreach my $line (split(/\n/, $res)) { + if ($line =~ /\*?\s+\d\s+(\w+)/) { + $confname = $1; + if ($line =~ /^\*/) { + $set = 1; + } + } + print "\n"; + print "$confname"; + print "
\n"; + $set = 0; +} + +print "\n"; +print "
"; + +print "
Upload new vcl file
"; +print ""; +print ""; +print "
"; +print "Config name: "; +print "
"; +print "VCL file: "; +print &ui_upload("vcl_file", 25); +print "
"; +print "\n"; +print "
"; +print ""; + +print "
"; + +&ui_print_footer("", 'module index'); diff --git a/varnish-tools/webmin/varnish_vcl_upload.cgi b/varnish-tools/webmin/varnish_vcl_upload.cgi new file mode 100644 index 00000000..c918f3fd --- /dev/null +++ b/varnish-tools/webmin/varnish_vcl_upload.cgi @@ -0,0 +1,30 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; +use File::Temp qw (mkstemp); +use Fcntl qw (F_SETFD F_GETFD :mode); + +&ReadParseMime(); +&error_setup("Failed"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'vcl.load'); + +my ($fh, $file) = mkstemp("/tmp/tmp.vcl.XXXXXXX"); +print $fh $in{'vcl_file'}; +close($fh); + +chmod S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, $file; + +$attrs{'params'} = "$in{'vcl_name'} $file"; + +my ($status, $res) = Varnish::CLI::send_command(\%attrs); + +if (!($status eq '200')) { + &error("$status - $res"); +} + +&redirect("varnish_vcl.cgi"); diff --git a/varnish-tools/webmin/varnish_vcl_use.cgi b/varnish-tools/webmin/varnish_vcl_use.cgi new file mode 100644 index 00000000..af28c9f4 --- /dev/null +++ b/varnish-tools/webmin/varnish_vcl_use.cgi @@ -0,0 +1,22 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ReadParse(); +&error_setup("Failed"); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'vcl.use'); + +$attrs{'params'} = "$in{'vcl_name'}"; + +my ($status, $res) = Varnish::CLI::send_command(\%attrs); + +if (!($status eq '200')) { + &error("$status - $res"); +} + +&redirect("varnish_vcl.cgi"); \ No newline at end of file diff --git a/varnish-tools/webmin/varnish_vcl_view.cgi b/varnish-tools/webmin/varnish_vcl_view.cgi new file mode 100644 index 00000000..8270cc64 --- /dev/null +++ b/varnish-tools/webmin/varnish_vcl_view.cgi @@ -0,0 +1,29 @@ +#!/usr/local/bin/perl + +# Author: Cecilie Fritzvold + +require './varnishadm-lib.pl'; +use Varnish::CLI; + +&ReadParse(); +&error_setup("Failed"); + +&ui_print_header(undef, "vcl file: $in{'vcl_file'}", "", undef, 1, 1); + +my %attrs = ('address' => $config{'address'}, 'port' => $config{'port'}, + 'command' => 'vcl.show'); + +$attrs{'params'} = "$in{'vcl_file'}"; + +my ($status, $res) = Varnish::CLI::send_command(\%attrs); + +if (!($status eq '200')) { + &error("$status - $res"); +} + +$res =~ s/ / /g; +$res =~ s/\n/
/g; + +print $res; + +&ui_print_footer("varnish_vcl.cgi", 'vcl index'); diff --git a/varnish-tools/webmin/varnishadm-lib.pl b/varnish-tools/webmin/varnishadm-lib.pl new file mode 100644 index 00000000..28994261 --- /dev/null +++ b/varnish-tools/webmin/varnishadm-lib.pl @@ -0,0 +1,14 @@ +#!/usr/local/bin/perl + +do '../web-lib.pl'; +&init_config(); +do '../ui-lib.pl'; + +if (!$config{'address'}) { + $config{'address'} = 'localhost'; +} +if (!$config{'port'}) { + $config{'port'} = 23; +} + +return 1;