Airflow UI link in SlackAPIPostOperator? - slack

Im using SlackAPIPostOperator in Airflow to send Slack messages on task failures.
I wondered if there's a smart way to add a link to the airflow UI logs page of the failed task to the slack message.
The following is an example I want to achieve:
http://myserver-uw1.myaws.com:8080/admin/airflow/graph?execution_date=...&arrange=LR&root=&dag_id=MyDAG&_csrf_token=mytoken
The current message is:
def slack_failed_task(context):
failed_alert = SlackAPIPostOperator(
task_id='slack_failed',
channel="#mychannel",
token="...",
text=':red_circle: Failure on: ' +
str(context['dag']) +
'\nRun ID: ' + str(context['run_id']) +
'\nTask: ' + str(context['task_instance']))
return failed_alert.execute(context=context)

You can build the url to the UI with the config value base_url under the [webserver] section and then use Slack's message format <http://example.com|stuff> for links.
from airflow import configuration
def slack_failed_task(context):
link = '<{base_url}/admin/airflow/log?dag_id={dag_id}&task_id={task_id}&execution_date={execution_date}|logs>'.format(
base_url=configuration.get('webserver', 'BASE_URL'),
dag_id=context['dag'].dag_id,
task_id=context['task_instance'].task_id,
execution_date=context['ts'])) # equal to context['execution_date'].isoformat())
failed_alert = SlackAPIPostOperator(
task_id='slack_failed',
channel="#mychannel",
token="...",
text=':red_circle: Failure on: ' +
str(context['dag']) +
'\nRun ID: ' + str(context['run_id']) +
'\nTask: ' + str(context['task_instance']) +
'\nSee ' + link + ' to debug')
return failed_alert.execute(context=context)

We can also do this using the log_url attribute in the Task Instance
def slack_failed_task(context):
failed_alert = SlackAPIPostOperator(
task_id='slack_failed',
channel="#mychannel",
token="...",
text=':red_circle: Failure on: ' +
str(context['dag']) +
'\nRun ID: ' + str(context['run_id']) +
'\nTask: ' + str(context['task_instance']) +
'\nLogs: <{url}|to Airflow UI>'.format(url=context['task_instance'].log_url)
)
return failed_alert.execute(context=context)
I know this is available since version 1.10.4 at the very least.

Related

Send realtime jmeter active threads on all slaves during remote testing to influxdb via jsr223 listner

How the send realtime jmeter active threads on all slaves during remote testing to influxdb via jsr223 listner
Any reason for not using JMeter's Backend Listener? This way you will get way more data without having to implement your custom solutions, you will have the number of active threads along with other metrics plotted:
If you still want an example Groovy code for the JSR223 Listener to add your custom metric with the custom tags to the custom database here is some sample you can use as a basis:
def influxHost = '192.168.99.100'
def influxPort = '8086'
def database = 'mydb'
def ip = org.apache.jmeter.util.JMeterUtils.getLocalHostIP()
def hostname = org.apache.jmeter.util.JMeterUtils.getLocalHostName()
def activeThreads = ctx.getThreadGroup().numberOfActiveThreads()
def client = org.apache.http.impl.client.HttpClientBuilder.create().build()
def post = new org.apache.http.client.methods.HttpPost('http://' + influxHost + ':' + influxPort + '/write?db=' + database)
def entity = new org.apache.http.entity.StringEntity('active_threads,host=' + hostname + ',ip=' + ip + ' value=' + activeThreads + ' ' + System.nanoTime())
post.setEntity(entity)
client.execute(post)
More information: Write data using the InfluxDB API

How to select a button from a interactive message with a bot?

I'm trying to get a bot to "click" a button on an interactive message in Slack (preferably as a bot, but using a user token works too).
I've found that the link to send the action information to be
https://blue-hybrid.slack.com/api/chat.attachmentAction
My problem is I can't find any documentation for "chat.attachmentAction." Looking at the request sent when using my browser, it has one http argument: "_x_id" and the payload is a WebKitForm, containing 4 items: payload, client_id, payload_id, and token.
I'm sure if I'm just not sending the appropriate data or authentication or what. All of my POSTs return "invalid_payload" or "invalid_arg_name."
Any help is greatly appreciated.
Looks like I figured it out, finally!
I had to work it out the old fashioned way. Slack Customer Support would only help with the official public API. I'll leave the solution here in Javascript.
To do this, you need 3 things:
choice_num
the number of the choice within the list of options.
e.g. If a message has the buttons (from left to right): yes, no, and maybe, then yes=0, no=1 and maybe=2.
message
the json of the interactive message
SLACK_TOKEN
your slack token (not sure if bot tokens work, user tokens do however)
The method chat.attachmentAction itself requires 3 arguments:
payload
service_id AND/OR bot_user_id
token
args = encodeURI(
'payload={'
+ '"actions":[' + JSON.stringify(message.attachments[0]["actions"][choice_num]) + '],'
+ '"attachment_id":"' + message.attachments[0]["id"] + '",'
+ '"callback_id":"' + message.attachments[0]["callback_id"] + '",'
+ '"channel_id":"' + message.channel + '",'
+ '"message_ts":"' + message.ts + '"}'
+ '&service_id=' + message.bot_id
+ '&bot_user_id=' + message.user
+ '&token=' + SLACK_TOKEN
)
request_url = 'https://YOURSLACKTEAM.slack.com/api/chat.attachmentAction?' + args
then just send an async POST to the request_url and you should get back something like this:
{"replaced":true,"ok":true}

URL working on localhost but not found on test server in Laravel

I have in my web.php a request as below:
Route::get('emp_listing/{region}/{start_date}/{end_date}/{supervisor?}/{emp_status?}', 'EmpListingController#get_emp_to_excel');
and in js I have the line below:
function getBaseUrl() {
var l = window.location;
var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1] + "/" + l.pathname.split('/')[2];
return base_url;
}
function getEmpExcelData(){
var base_url = getBaseUrl();
window.location.href = base_url + "/emp_listing/" + region + '/' + start_date + '/' + end_date + '/' + supervisor + '/' + emp_status;
}
On my local computer when I click the button, the link in js execute successfully however on test server, it says
Firefox can’t find the file at http://xx.69.1x.xx/emp/public/emp_listing/1/2017-11-01/2017-11-30/0/null
I don't understand why this works locally but doesn't work on test server.
Every other request is working fine
After digging, I found my problem. I am using Maatwebsite\Excel package. On my local server, the php zip extension is installed but not on test server. So I have installed the extension like below:
sudo yum install php-pecl-zip
After installing, everything now works fine.

Changing container managed authentification alias

I'm using WebSphere 7.0.0.37 and jython
I need to change the 'Container-managed authentication alias', unfortunatelly I can't find anything in API, inspecting attributes of existing DataSources or any example for that task.
I have succesfully changed the 'composant-managed authentication alias' with:
AdminConfig.modify(DataSourceProvider, '[[name "basename"] [authDataAlias "' + nameNode + '/' + aliasJaas + '" ] ')
How can i do that?
thank you!
Here is some logic which you could use to solve your problem.
# Create new alias
cellName = AdminConfig.showAttribute(AdminConfig.list("Cell"), "name")
security = AdminConfig.getid('/Cell:' + cellName + '/Security:/')
myAlias = 'blahAlias'
user = 'blah'
pswd = 'blah'
jaasAttrs = [['alias', myAlias], ['userId', user], ['password', pswd ]]
print AdminConfig.create('JAASAuthData', security, jaasAttrs)
print "Alias = " + myAlias + " was created."
# Get a reference to your DataSource (assume you know how to do this):
myDS = ...
# Set new alias on DataSource
AdminConfig.modify('MappingModule', myDS, '[[authDataAlias ' + myAlias + '] [mappingConfigAlias DefaultPrincipalMapping]]')
Note that if you can figure out how to do a given task in the Admin Console, you can use the "Command Assist" function to get a Jython snippet to do the equivalent via wsadmin. See here.

Jython with wsadmin: WASX7017E com.ibm.ws.scripting.ScriptingException: Invalid object name

I have a Jython script calling wsadmin libraries to configure a WAS server.
I have these functions:
def createWasObject(was_object_type, was_path, object_params):
if isinstance(was_path, basestring):
was_path = AdminConfig.getid(was_path)
str_params = '['
for k,v in object_params.items():
str_params = str_params + '[' + k + ' "' + v + '"] '
str_params = str_params + ']'
return AdminConfig.create(was_object_type, was_path, str_params)
def createJdbcProviders(was_path, jdbc_providers):
was_object_type = 'JDBCProvider'
for jdbc_provider in jdbc_providers:
jdbc = createWasObject(was_object_type, was_path, jdbc_provider['params'])
print jdbc
for datasource in jdbc_provider['datasources']:
ds = createWasObject('Datasource', jdbc, datasource['params'])
print
The "print jdbc" prints:
Teradata JDBC Provider(cells/jsr-websphere-1Cell01/nodes/jsr-websphere-1Node01/servers/jsr-business|resources.xml#JDBCProvider_1444648929602)"
Which looks like a correct object ID
However, when using it to create a datasource, I get the following error:
WASX7017E: Exception reçue lors de l'exécution du fichier "/root/jsr_auto_deployment/jsr.py" ; informations sur l'exception : com.ibm.ws.scripting.ScriptingException: Invalid object name: "Teradata JDBC Provider(cells/jsr-websphere-1Cell01/nodes/jsr-websphere-1Node01/servers/jsr-business|resources.xml#JDBCProvider_1444648929602)"
I am using Jython 2.7 through a Thin client. Reusing an object returned by AdminConfig.create() was working well with a Jython script ran through wsadmin.sh
My problem was on that line:
was_path = AdminConfig.getid(was_path)
Most of the time I was passing a string but this time I was already using an ID. So I removed the AdminConfig.getid in the function and added it in the calls when necessary only.

Resources