on 04-15-2018 04:20 AM
In this post I would like to explain how to create a small POC together with a Spark bot and the Devnet Sandbox IOS XE on Catalyst 9000.
In this POC you will be able to showcase 2 main concepts:
Go to the following link and reserve a IOS XE on Catalyst 9000 sandbox to work on. It may take around 10 minutes for the entire process to finish.
Go to https://developer.ciscospark.com/apps.html and create a new bot. Click in My Apps > Create a Bot and later to the add new application:
Create the new Bot with relevant information and make sure you write down Bot's access token.
mylaptop:~ laptop$ ssh admin@10.10.20.100
>cat9300-2#guestshell enable
Management Interface will be selected if configured
Please wait for completion
guestshell is in RUNNING state
>cat9300-2#guestshell
[guestshell@guestshell ~]$
>cat9300-2#guestshell destroy
Guestshell destroyed successfully
>cat9300-2#guestshell enable
Management Interface will be selected if configured
Please wait for completion
Guestshell enabled successfully
>cat9300-2#guestshell
[guestshell@guestshell ~]$
First we need to update the DNS configuration with a proper DNS server to resolve DNS queries. Let's use OpenDNS::208.67.222.222
[guestshell@guestshell ~]$ sudo echo "nameserver 208.67.222.222" | sudo tee --append /etc/resolv.conf
nameserver 208.67.222.222
Following, we need to update repositories, libraries and needed tools. Make sure you type "yes / y" when asked and allow the installation of needed packages. (You can ignore some of the errors presented when installing different modules)
[guestshell@guestshell ~]$ sudo yum update
[...]
Complete!
[guestshell@guestshell ~]$ sudo pip install --upgrade pip
[...]
Successfully installed pip-10.0.0
[guestshell@guestshell ~]$ sudo pip install -U setuptools
[...]
Successfully installed setuptools-39.0.1
Now that we have the guestshell ready we can install Cisco Spark API:
[guestshell@guestshell ~]$ sudo -E pip install ciscosparkapi
[...]
Successfully installed certifi-2018.1.18 chardet-3.0.4 ciscosparkapi-0.10 future-0.16.0 idna-2.6 requests-2.18.4 requests-toolbelt-0.8.0 urllib3-1.22
With the guestshell ready it's time to try a script. (( Thanks @HaroldPreston for putting this together in your post ))
- from ciscosparkapi import CiscoSparkAPI
- if __name__=='__main__':
- # Use ArgParse to retrieve command line parameters.
- from argparse import ArgumentParser
- parser = ArgumentParser("Spark Check In")
- # Retrieve the Spark Token and Destination Email
- parser.add_argument(
- "-t", "--token", help="Spark Authentication Token", required=True
- )
- # Retrieve the Spark Token and Destination Email
- parser.add_argument(
- "-e", "--email", help="Email to Send to", required=True
- )
- args = parser.parse_args()
- token = args.token
- email = args.email
- message = "**Alert:** Config Changed"
- api = CiscoSparkAPI(access_token=token)
- api.messages.create(toPersonEmail=email, markdown=message)
We need to create a .py file with the above code in order to be executed by the guestshell and reach to Spark. In this example we will create "myscript.py"
1) COPY the script text above: select the text, right click copy
2) Create new file, for example myscript.py Let's use here for example the vi editor:
[guestshell@guestshell ~]$ sudo vi myscript.py
3) Once you enter vi, press "i" to get into insert mode, right click into terminal, click paste.
4) Press key ESC and type :wq! then press ENTER to save all the document.
~
~
"myscript.py" [New] 20L, 780C written
[guestshell@guestshell ~]$
At this point we should have our "myscript.py" in the Catalyst 9300 guestshell. We can verify it by typing any of the commands below:
[guestshell@guestshell ~]$ ls
myscript.py
[guestshell@guestshell ~]$ cat myscript.py
from ciscosparkapi import CiscoSparkAPI
if __name__=='__main__':
# Use ArgParse to retrieve command line parameters.
from argparse import ArgumentParser
parser = ArgumentParser("Spark Check In")
# Retrieve the Spark Token and Destination Email
parser.add_argument(
"-t", "--token", help="Spark Authentication Token", required=True
)
# Retrieve the Spark Token and Destination Email
parser.add_argument(
"-e", "--email", help="Email to Send to", required=True
)
args = parser.parse_args()
token = args.token
email = args.email
message = "**Alert:** Config Changed"
api = CiscoSparkAPI(access_token=token)
api.messages.create(toPersonEmail=email, markdown=message)
Now that the script is in the guestshell is time to run it and verify that is working:
sudo python myscript.py -t "Bot's access token." -e "our email where spark is enabled"
For example:
[guestshell@guestshell ~]$ sudo python myscript.py -t ZjZjMTkxY2ItYTUxNy00MGJjLTg1MDEtNzQ5MzgyOWEyOTcwZjM0NmE2NzItNDI2 -e cferrate@cisco.com
As a result we should see a Spark notification from our Bot:
At this point we have validated that the Catalyst9300 guesthsell is able to send messages to our Spark account using the Bot created previously.
With the script working, now we will make the Catalyst 9300 to execute the script and send a message over Spark every time a Syslog Message related to a configuration change is issued. For this, we will use above script as an action to a EEM event.
(( Thanks again @HaroldPreston for putting this together in your post ))
First we need to exit the guestshell
[guestshell@guestshell ~]$ exit
exit
cat9300-2#
Now we configure the EEM:
cat9300-2#config t
cat9300-2(config)#event manager applet testapplet
cat9300-2(config-applet)#event syslog pattern "%SYS-5-CONFIG_I: Configured from"
cat9300-2(config-applet)#action 0.0 cli command "en"
cat9300-2(config-applet)#action 1.0 cli command "guestshell run python myscript.py -t ZjZjMTkxY2ItYTUxNy00MGJjLTg1MDEtNzQ5MzgyOWEyOTcwZjM0NmE2NzItNDI2 -e cferrate@cisco.com"
cat9300-2(config-applet)#exit
cat9300-2(config)#exit
With this every time there is a system configuration change syslog message the EEM will run the script in the guestshell and this notify us over spark of a configuration change.
Be aware that EEM event pattern support regex so you could potentially match any type of variable in the syslog messages using the exact same approach as above.
Great example of using DevNet Sandbox, Cat 9K and Spark APIs for some ChatOps action! Love it!
Thanks for your contribution, Charles! We've added your POC/post as a example within the instructions of the IOS XE on Catalyst 9000 lab.
Attempting the transition from learning Labs to sandbox. Tried other Labs without success. Everything spelled out in this Lab (and I have Cisco Spark). Thanks for being so thorough, I can now attempt the other labs.
Thanks Dave; happy to help
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: