How Syslog can lose data

From Rainer’s Blog. It looks syslog assumes data sent whenTCP send() was successful by design.

Simulated the behavior and confirmed by running below one in client mode & the other in server mode. When you kill the server process & run it again while client process is running, you’ll see a message lost.

import sys
import SocketServer
import socket
import time

def usage():
    print "%s [client | server]" % sys.argv[0]
    
def server(HOST, PORT):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((HOST, PORT))
    sock.listen(1)
    (conn, sa) = sock.accept()
    try:
        while True:
            msg = conn.recv(8192)
            print "* received '%s'" % msg
            if len(msg) == 0:
                break;
    except socket.error, e:
        print 'Error: %s' % e
    
    conn.close()

def client_connect(HOST, PORT):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect((HOST, PORT))
    except socket.error, e:
        print "Error: %s" % e
        return None
    
    return sock

def client(HOST, PORT):
    sock = None
    i = 0
    while i< 10:
        if sock == None:
            print "connecting..."
            sock = client_connect(HOST, PORT)
        if sock == None:
            print "no connection. sleeping..."
            time.sleep(3)
            continue
        
        data = "* hoge %d" % i
        print "sending %s" % data
        
        try:
            ret = sock.send(data)
            
            print "Sent:     %s, return: %d" % (data, ret)
            i = i + 1
        except socket.error, e:
            print 'Error: %s' % e
            sock.close()
            sock = None
        print "sleeping..."
        time.sleep(1)

    sock.shutdown(socket.SHUT_RDWR)
    sock.close()

def main():
    if len(sys.argv) != 2:
        usage()
        return

    HOST, PORT = "localhost", 9999

    if sys.argv[1].lower() == "server":
        print "running server..."
        server(HOST, PORT)
    else:
        print "running client..."
        client(HOST, PORT)

if __name__ == "__main__":
    main()

Zabbix Agent for Windows

Downloaded the latest Windows binary and ran it with windows config in the source tar file.

Defined server address – one for passive and the other for active monitoring. Then added perf counter and files.

Server=hogehoge
ServerActive=hogehoge

Created “Windows Eventlog” application to store event logs.

Successfully configured file monitor. Notice that no single or double quote supported!

 log[c:\users\sokoide\hoge.log]

Zabbix Install

Installed Zabbix on ubuntu on Parallels since I didn’t mess up Mac env.

install:

sudo apt-get install zabbix-server-mysql -y
sudo apt-get install zabbix-agent -y
sudo apt-get install zabbix-frontend-php -y

config files:

/etc/zabbix/zabbix_server.conf
/etc/zabbix/zabbix_agent.conf

default log path:

/var/log/zabbix

web frontend:

http://$hostname/zabbix

(default username: Admin, password: zabbix)

stop/start:

sudo service zabbix-server start
sudo service zabbix-server stop

Will configure Windows agent later.

ZeroMQ PUB/SUB memo

def sub_handler(sock, events):
    msg = sock.recv()
    print msg

def sub():
    print "*** sub"
    loop = ioloop.IOLoop.instance()
    ctx = zmq.Context()
    sock = ctx.socket(zmq.SUB)
    sock.connect('tcp://127.0.0.1:5555')
    sock.setsockopt(zmq.SUBSCRIBE, '')
    loop.add_handler(sock, sub_handler, zmq.POLLIN)
    loop.start()

def pub():
    print "*** pub"
    ctx = zmq.Context()
    sock = ctx.socket(zmq.PUB)
    sock.bind('tcp://127.0.0.1:5555')
    for i in range(0,10):
        msg = "[%d] Hello" % i
        print "publishing %s" % msg
        sock.send(msg)
        time.sleep(0.5)

ZeroMQ REQ/REP memo

ZeroMQ request/reply memo.
install:

sudo port install py-zmq

py:

import sys
import zmq
from zmq.eventloop import ioloop

def rep_handler(sock, events):
    msg = sock.recv()
    sock.send(msg)

def rep():
    print "*** rep"
    loop = ioloop.IOLoop.instance()
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REP)
    sock.bind('tcp://127.0.0.1:5555')
    loop.add_handler(sock, rep_handler, zmq.POLLIN)
    loop.start()

def req():
    print "*** req"
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REQ)
    sock.connect('tcp://127.0.0.1:5555')
    sock.send('hello')
    print sock.recv()

def main():
    command = sys.argv[1].lower()
    if command == "req":
        req()
    elif command == "rep":
        rep()

main()

node.js memo

install:

sudo port install nodejs
sudo port install netcat

tcp echo server js:

var net = require("net");
net.createServer(function(sock){
    sock.on("data", function(data){
        sock.write(data);
    });
}).listen(8000);

http echo server js:

var http = require("http");
http.createServer(function(req, res){
    res.writeHead(200, {'Content-type' : 'text/plain'});
    res.write("Hello\n");
    res.end();
}).listen(8000);

test client command:

nc localhost 8000
curl http://localhost:8000