#!/bin/bash

sudo apt-get install pen

cat <<EOF|sudo tee /etc/systemd/system/pen.service
[Unit]
Description=light weight simple load balancer
 
[Service]
ExecStartPre=/bin/mkdir -p /var/log/pen
ExecStart=/usr/bin/pen -f -F/etc/pen.conf -p /var/run/pen.pid -l /var/log/pen/pen.log 3306
ExecStartPost=/bin/echo "started pen at `date`"
Restart=on-failure
RestartSec=1
 
[Install]
WantedBy=multi-user.target
EOF
 
 
cat <<EOF|sudo tee /etc/pen.conf
log /var/log/pen/pen.log
no http
roundrobin
timeout 10
web_stats /var/log/pen/stats.log
no block
listen 3306
server 0 address 192.169.1.200 port 3306
server 1 address 192.169.6.25 port 3306
server 2 address 192.169.6.25 port 3306
EOF
  
sudo systemctl daemon-reload
sudo systemctl enable pen
sudo service pen start