#!/usr/bin/env python

#   Copyright 2009 Oli Schacher
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $Id$
#

# This tool is used to send messages to the fuglu control port

import sys
import socket

if len(sys.argv)<2:
    print ""
    print "Usage fuglu_control <command> [<args>]"
    print "Available commands:"
    print "stats : show statistics"
    print "uptime: show long fuglu has been running"
    print "workerlist: show current status of all mail scanning threads"
    print "threadlist: show current status of ALL threads (core + workers)"
    print "exceptionlist: last 10 exception tracebacks"
    sys.exit(1)


localport=10010

cmd=" ".join(sys.argv[1:])
#connect to debug port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(('127.0.0.1', localport))
except Exception,e:
    print "Cannot connect to fuglu control deamon on port %s - is fuglu running?"%localport
    print str(e)
    sys.exit(1)
    
s.sendall(cmd)
data = s.recv(32768)
s.close()
print data

