Messaging automation (deprecated)

Prev Next

How to Automatically Launch Messagings (only for versions prior to 4.1)

ZSA does not provide the ability to launch the service automatically. However, it is possible to use scheduled task to call the ZSA API and launch them.

Windows Scheduler

You can use Windows Task Scheduler to automate your messaging task. All you need is the possibility to call the ZSA url.

For this example, we are going to use the curl library.

 1. Create a new script called startmessaging.cmd with the following line:

curl -k -X POST https://ZETALY_HOST/zsa/api/v1/servers/MESSAGING_NAME/messaging/start

Replace ZETAY_HOST by your Zetaly host, and MESSAGING_NAME by the messaging name to launch. Repeat the line for each messaging you want to launch.

2. Create the task. Run the following command in an Administrative console: 

schtasks /CREATE /TN ZetalyMessaging /RU "SYSTEM" /SC daily /ST 05:00 /TR "PATH_TO_SCRIPT\startmessaging.cmd"

Replace PATH_TO_SCRIPT with the folder used to create the script in step 1.

You can replace the /ST and /SC parameters by the hour and periodicity you want. See windows documentation for more details.

Docker (crontab-ui)

Get the Image

To schedule messaging using a Docker container, Zetaly recommend to use crontab-ui

If needed, Zetaly provides a s390x image for ZCX : 


Load the tar file:

docker load -i crontab-ui.tar.gz

Run the Container

Once the image is available on your docker, you can run a container with crontab-ui:

docker run -d -p 8000:8000 alseambusher/crontab-ui

Scheduling

At the top of the ui, use "New":

Create a job with the desired scheduling. This job should run first following command line:

token=$(curl -k -H "Accept: application/json" -H "Content-type: application/json" -X POST https://ZETALY_HOST/zhb/api/v1/users/public/login -d '{"username":"USERNAME","password":"PASSWORD"}' | python3 -c "import sys, json; print(json.load(sys.stdin)['token'])");

Replace ZETAY_HOST by your Zetaly host, and USERNAME/PASSWORD by your user credentials.

Technical User
It is recommend to create a specific user to execute this script. If a generic one is used, it will disconnect it.

After this first line, add as many lines as needed using the the following command line:

curl -ki -H "token: $token" -X POST https://ZETALY_HOST/zsa/api/v1/servers/MESSAGING_NAME/messaging/start

Replace ZETAY_HOST by your zetaly host, and MESSAGING_NAME by the messaging name to launch.

Note: Host IP
Docker provides the host named "host.docker.internal" if you want to refer to the Docker host inside a container.
Note: Multiple commands
You can add several commands separated by ;

Example, every hour:

Once the job is created, you can try it:

When the job is working, you can save it to cron:


Linux

Prerequisite
For this example, the linux installation must have:
  • Crontab
  • Bash
  • Python 3
  • Curl

Script

Create a script as followed:

# To update
action=start # start/stop
zetaly_host=https://host
zetaly_user=user
zetaly_password=pwd
# List all messaging or parser to start. Do no use coma !
messagings=(CSV_PARSER RAW_PARSER)
# Set to 1 for more information
debug=0

#####################################
# Login (get token)
#####################################
body="{\"username\":\"$zetaly_user\",\"password\":\"$zetaly_password\"}"
url="$zetaly_host/zhb/api/v1/users/public/login"
curlCmd="curl -s -k -H \"Accept: application/json\" -H \"Content-type: application/json\" $url -d '$body' --trace './login.log' -o './login.json'"

if [ "$debug" = 1 ]; then
        echo "***********"
        echo "Url to call: $url"
        echo "Body to send: $body"
        echo "Curl command to execute: $curlCmd"
        echo "***********"
fi

eval $curlCmd
result=$(cat ./login.json)

if [ "$debug" = 1 ]; then
        echo "***********"
        echo "Curl result: $result"
        echo "***********"
fi

token=$(echo $result | python3 -c "import sys, json; print(json.load(sys.stdin)['token'])")

if [ "$debug" = 1 ]; then
        echo "***********"
        echo "Token: $token"
        echo "***********"
fi


#################################
# Start messaging if logged
#################################
if [ -n "$token" ]; then
        for messaging in "${messagings[@]}"
        do
                url="$zetaly_host/zsa/api/v1/servers/$messaging/messaging/$action"
                curlCmd="curl -ki -X POST $url -H 'token: $token' -H 'accept: */*' -d ''"
                 if [ "$debug" = 1 ]; then
                        echo "***********"
                        echo "Messaging: $messaging"
                        echo "Url to call: $url"
                        echo "Curl command to execute: $curlCmd"
                        echo "***********"
                fi
                eval $curlCmd
        done
else
        echo "!!!!!!!!!!!!!!!! Login failed !!!!!!!!!!!!!!!!"
fi
echo ""

At the beginning, edit the following lines:

zetaly_host=https://host
zetaly_user=user
zetaly_password=pwd
messagings=(CSV_PARSER RAW_PARSER)

Test the script using the command:

bash './myscript'

Display example:

HTTP/1.1 200 OK
X-Powered-By: Express
content-length: 0
connection: close
date: Tue, 23 Jul 2024 09:33:05 GMT
server: Kestrel

HTTP/1.1 200 OK
X-Powered-By: Express
content-length: 0
connection: close
date: Tue, 23 Jul 2024 09:33:05 GMT
server: Kestrel

Crontab

Create a crontab using:

crontab -e

Add a line with your script execution:

* * * * * bash /home/myuser/script/start-connectors.sh
Cron format
If you need help to create your cron format, you can use https://crontab.guru/

Once edited, the script will be launched according to your cron.

You can check logs using:

grep 'home/myuser/script/start-connectors' /var/log/syslog


How to mass create connector

It is sometimes necessary to create a large number of connectors. You can automate the creation process by using ZSA's Rest APIs.

All APIs are available and can be viewed at this address: /zsa/api/v2/q/swagger-ui/

The creation of a connector use the POST API /zsa/api/v2/connectors

Example of full body:

{
   "label":"Lpar",
   "type":"CSV",
   "host":"45.21.456.89",
   "port":9999,
   "autoStart":true,
   "numberOfRetries":2,
   "retryDelay":30,
   "tags":[
      
   ],
   "properties":[
      {
         "name":"NbInstances",
         "value":"1"
      },
      {
         "name":"MsgSize",
         "value":"5000"
      },
      {
         "name":"ConnectionSleepTime",
         "value":"5"
      },
      {
         "name":"AutoScaleInstances",
         "value":"false"
      }
   ]
}

All attributes are not mandatory, the mandatory attributes are the following:

{
   "label":"Lpar",
   "type":"CSV",
   "host":"45.21.456.89",
   "port":9999
}

Example of shell script to mass create connectors:

# To update
zetaly_host=https://zetaly
zetaly_user='user'
zetaly_password='password'
# List all connecteur/hosts/port
name=(Lpar1 Lpar2)
host=(localhost localhost2)
port=(9999 9999)
# Connector types to create
types=(CSV RAW)
# Set to 1 for more information
debug=0

#####################################
# Check values
#####################################
if [ "${#name[@]}" != "${#host[@]}" ] || [ "${#name[@]}" != "${#port[@]}" ]; then
	echo "Error: All arrays does not have the same number of elements."
	exit 8
fi
if [ "${#types[@]}" = "0" ]; then
	echo "Error: Type arrays does not hae any value."
	exit 8
fi

#####################################
# Login (get token)
#####################################
body="{\"username\":\"$zetaly_user\",\"password\":\"$zetaly_password\"}"
url="$zetaly_host/zhb/api/v1/users/public/login"
curlCmd="curl -s -k -H \"Accept: application/json\" -H \"Content-type: application/json\" $url -d '$body' --trace './login.log' -o './login.json'"

if [ "$debug" = 1 ]; then
	echo "***********"
	echo "Url to call: $url"
	echo "Body to send: $body"
	echo "Curl command to execute: $curlCmd"
	echo "***********"
fi

eval $curlCmd
result=$(cat ./login.json)

if [ "$debug" = 1 ]; then
	echo "***********"
	echo "Curl result: $result"
	echo "***********"
fi

token=$(echo $result | python3 -c "import sys, json; print(json.load(sys.stdin)['token'])")

if [ "$debug" = 1 ]; then
	echo "***********"
	echo "Token: $token"
	echo "***********"
fi


#################################
# Create messaging
#################################
if [ -n "$token" ]; then
	url="$zetaly_host/zsa/api/v2/connectors"
	maxIndex="${#name[@]}"
	forExpr={1..$maxIndex}
	for (( i=0; i<maxIndex; i++))
	do
		connectorName="${name[i]}" 
		connectorHost="${host[i]}" 
		connectorPort="${port[i]}"
		for type in "${types[@]}"
		do
			body="{\"label\":\"$connectorName\",\"type\":\"$type\",\"host\":\"$host\",\"port\":$port, \"autoStart\":true}"
            curlCmd="curl -s -k -H 'token: $token' -H \"Accept: application/json\" -H \"Content-type: application/json\" $url -d '$body'"
			if [ "$debug" = 1 ]; then
				echo "***********"
				echo "Messaging: $messaging"
				echo "Url to call: $url"
				echo "Curl command to execute: $curlCmd"
				echo "***********"
			fi
			eval $curlCmd
		done
	done
else
	echo "!!!!!!!!!!!!!!!! Login failed !!!!!!!!!!!!!!!!"
fi
echo ""