Configuring a monitor for IBM Cloud Functions
IBM Cloud Functions is a functions-as-a-service (FaaS) programming platform that you can use to develop lightweight code and run the app logic on demand. For more information on IBM Cloud Functions, see Getting started with IBM Cloud Functions.
This example demonstrates how to create an action in IBM Cloud Functions using the Python script. You can then use the created action to define an SMTP service to send an email to the specified email address whenever a deviation occurs
Before you begin
You can use the following steps to create an action in IBM Cloud Functions:
-
In the IBM Cloud application, navigate to the IBM Cloud Functions > Actions.
-
On the Actions page, click Create.
If you are using the IBM Cloud Functions catalog for the first time, you are prompted to create a namespace before creating an action.
-
In the Action Name field of the Create Action page, type SMTP_MESSAGE, and then in the Runtime field, select Python version number. Click Create.
-
In the Code page of the creation wizard, type the following script:
import sys import smtplib from email.message import EmailMessage from email.utils import formataddr def main(dict): if "encription" in dict: encription = dict["encription"] else: encription = "SSL" # set parameters server = dict["server"] port = dict["port"] email_address = dict["email"] email_password = dict["pass"] email_from = dict["from"] sender = formataddr(('%s', '%s')) % (email_from, email_address) to = dict["to"] cc = dict["cc"] subject = dict["subject"] content = dict["content"] # create email msg = EmailMessage() msg['Subject'] = subject msg['From'] = sender msg['To'] = to msg['Cc'] = cc msg.set_content(content, subtype='html') if encription == "TLS": # send email in TLS with smtplib.SMTP( server, port) as smtp: smtp.ehlo() # send the extended hello to our server smtp.starttls() # tell server we want to communicate with TLS encryption smtp.login(email_address, email_password ) smtp.send_message(msg) smtp.quit() return { 'message': 'TLS done' } else: # send email in SSL with smtplib.SMTP_SSL( server, port) as smtp: smtp.login(email_address, email_password) smtp.send_message(msg) smtp.quit() return { 'message': 'SSL done' }
-
In the Endpoints page of the wizard, select the Enable as Web Action checkbox, and then copy the URL indicated in the following figure.
Creating an activity monitor using an IBM Cloud Functions service
Use the following steps to configure an Activities monitor with the Slack service in IBM Process Mining:
-
Open a project and then go to the Action Hub page.
-
On the Action Hub page, click Create service.
-
In the Create new service dialog, ensure that the Service Type is Rest API, and then in the Service title field, type the name of the service (for example Service for IBM cloud functions).
-
In the Description field, type a description for the service that you create.
-
In the URL field, type the URL obtained from IBM Cloud Functions*.
-
Set Allow self-signed certificates to True.
-
In the Authentication type section, click No Auth, and then click Create.
-
On the Action hub page, click Create monitor.
-
In the Details step of creating a monitor, specify the name for the activity monitor, provide a description for the monitor, and then click Next.
-
In the Monitor step, complete the following steps:
a. In the Monitor type field, select Activities.
b. Select the activity to monitor
c. Click Manage filters to add a filter to the monitor. For more information on how to apply filters on monitors, see Applying filters in Action Hub.
d. Click Next. -
In the Schedule step, define the criteria for running the monitor, and then click Next.
-
In the Service step, select the service that is created by using IBM Cloud Functions, and then click Next.
-
In the Parameters step, do the following steps:
a. In the URL suffix field, type a name for the page in which you want to display the results of the monitor event.
b. In the HTTP method field, select POST.
c. In the Body parameters section, click Customised Body, and then type the following
json
script in the field:{ "server": "smtp.gmail.com", "port": "465", "email": "<your_email@gmail.com>", "pass": "<your_gmail_app_password>", "from": "IPM Bot", "to": "<someone@email.com>", "cc": "", "subject": "msg from IPM", "content": "this is the message from IPM" }
The results of this monitor event is send to the specified Gmail account.
d. Click **Verify Connection** to check the connection, and then click **Create**.
- On the Action Hub page, click the three dots button on the tab of the created monitor, and then click Run.
For more information on the fields in the Create Activity monitor wizard and its related values, see Setting a monitor for Activities.