Mailing List Archive

r1502 - in trunk/varnish-cache/bin: . varnishadm
Author: cecilihf
Date: 2007-06-07 11:00:56 +0200 (Thu, 07 Jun 2007)
New Revision: 1502

Added:
trunk/varnish-cache/bin/varnishadm/
trunk/varnish-cache/bin/varnishadm/Makefile.am
trunk/varnish-cache/bin/varnishadm/varnishadm.1
trunk/varnish-cache/bin/varnishadm/varnishadm.c
Log:
Added a utility for sending a single command to varnishd via telnet, and printing the result.



Property changes on: trunk/varnish-cache/bin/varnishadm
___________________________________________________________________
Name: svn:ignore
+ .deps
.libs
Makefile
Makefile.in
varnishadm


Added: trunk/varnish-cache/bin/varnishadm/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishadm/Makefile.am (rev 0)
+++ trunk/varnish-cache/bin/varnishadm/Makefile.am 2007-06-07 09:00:56 UTC (rev 1502)
@@ -0,0 +1,17 @@
+# $Id$
+
+INCLUDES = -I$(top_srcdir)/include
+
+bin_PROGRAMS = varnishadm
+
+dist_man_MANS = varnishadm.1
+
+varnishadm_SOURCES = \
+ varnishadm.c
+
+varnishadm_CFLAGS = -include config.h
+
+varnishadm_LDADD = \
+ $(top_builddir)/lib/libcompat/libcompat.a \
+ $(top_builddir)/lib/libvarnish/libvarnish.la
+


Property changes on: trunk/varnish-cache/bin/varnishadm/Makefile.am
___________________________________________________________________
Name: svn:keywords
+ Id

Added: trunk/varnish-cache/bin/varnishadm/varnishadm.1
===================================================================
--- trunk/varnish-cache/bin/varnishadm/varnishadm.1 (rev 0)
+++ trunk/varnish-cache/bin/varnishadm/varnishadm.1 2007-06-07 09:00:56 UTC (rev 1502)
@@ -0,0 +1,72 @@
+.\"-
+.\" Copyright (c) 2006 Verdens Gang AS
+.\" Copyright (c) 2006 Linpro AS
+.\" All rights reserved.
+.\"
+.\" Author: Cecilie Fritzvold <cecilihf at linpro.no>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $Id$
+.\"
+.Dd June 06, 2007
+.Dt VARNISHADM 1
+.Os
+.Sh NAME
+.Nm varnishadm
+.Sh SYNOPSIS
+.Nm
+.Fl T Ar address:port <command>
+.Sh DESCRIPTION
+The
+.Nm
+utility sends the given command to the
+.Xr varnishd 1
+instance at address:port and prints the results. 0 is returned on success, 1
+on failure.
+.Pp
+The following options are available:
+.Bl -tag -width Fl
+.It Fl T Ar address:port
+Connect via telnet to this address and port.
+.Sh EXAMPLES
+The following command lists all available commands provided by the
+management interface of
+.Ed
+.Xr varnishd 1
+.Bd -literal -offset 4n
+$ varnishadm -T 127.0.0.1:23 help
+.Ed
+.Sh SEE ALSO
+.Xr varnishd 1 ,
+.Xr varnishhist 1 ,
+.Xr varnishncsa 1 ,
+.Xr varnishstat 1 ,
+.Xr varnishtop 1
+.Sh HISTORY
+The
+.Nm
+utility was developed by
+.An Cecilie Fritzvold Aq cecilihf at linpro.no .
+
+This manual page was written by
+.An Cecilie Fritzvold Aq cecilihf at linpro.no .

Added: trunk/varnish-cache/bin/varnishadm/varnishadm.c
===================================================================
--- trunk/varnish-cache/bin/varnishadm/varnishadm.c (rev 0)
+++ trunk/varnish-cache/bin/varnishadm/varnishadm.c 2007-06-07 09:00:56 UTC (rev 1502)
@@ -0,0 +1,155 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006 Linpro AS
+ * All rights reserved.
+ *
+ * Author: Cecilie Fritzvold <cecilihf at linpro.no>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "libvarnish.h"
+#include "vss.h"
+
+#define STATUS_OK 200
+
+/*
+ * This function establishes a connection to the specified ip and port and
+ * sends a command to varnishd. If varnishd returns an OK status, the result
+ * is printed and 0 returned. Else, an error message is printed and 1 is
+ * returned
+ */
+static void
+telnet_mgt(const char* T_arg, int argc, char* argv[])
+{
+ struct vss_addr **ta;
+ char *addr, *port;
+ int i, n;
+ long status, bytes;
+ char *answer = NULL;
+ char buf[13];
+ char *p, *pp;
+
+ XXXAZ(VSS_parse(T_arg, &addr, &port));
+ XXXAN(n = VSS_resolve(addr, port, &ta));
+ free(addr);
+ free(port);
+ if (n == 0) {
+ fprintf(stderr, "Could not open TELNET port\n");
+ exit(2);
+ }
+
+ int sock = VSS_connect(ta[0]);
+
+ for (i = 0; i < n; ++i) {
+ free(ta[i]);
+ ta[i] = NULL;
+ }
+ free(ta);
+
+ for (i=0; i<argc; i++) {
+ if (i > 0)
+ write(sock, " ", 1);
+ write(sock, argv[i], strlen(argv[i]));
+ }
+ write(sock, "\n", 1);
+
+ n = read(sock, buf, 13);
+ if (n != 13) {
+ fprintf(stderr, "An error occured in receiving status.\n");
+ exit(1);
+ }
+ if (!(p = strchr(buf, ' '))) {
+ fprintf(stderr, "An error occured in parsing of status code.\n");
+ exit(1);
+ }
+ *p = '\0';
+ status = strtol(buf, &p, 10);
+ pp = p+1;
+ if (!(p = strchr(pp, '\n'))) {
+ fprintf(stderr, "An error occured in parsing of number of bytes returned.\n");
+ exit(1);
+ }
+ *p = '\0';
+ bytes = strtol(pp, &p, 10);
+
+ answer = malloc(bytes+1);
+ n = read(sock, answer, bytes);
+ if (n != bytes) {
+ fprintf(stderr, "An error occured in receiving answer.\n");
+ exit(1);
+ }
+ answer[bytes] = '\0';
+ close(sock);
+
+ if (status == STATUS_OK) {
+ printf("%s\n", answer);
+ exit(0);
+ }
+ fprintf(stderr, "Command failed with error code %ld\n", status);
+ exit(1);
+
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: varnishadm -T address:port <command> \n");
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int c;
+ const char *address = NULL;
+ int T_arg = 0;
+
+ if (argc < 2)
+ usage();
+
+ while ((c = getopt(argc, argv, "T:")) != -1) {
+ switch (c) {
+ case 'T':
+ T_arg = 1;
+ address = optarg;
+ break;
+ default:
+ usage();
+ }
+ }
+
+ if (T_arg) {
+ if (optind == argc)
+ usage();
+ telnet_mgt(address, argc - optind, &argv[optind]);
+ }
+
+ exit(0);
+}