2020-02-12

Changing Slack status from command-line

I'm used to track mine working time with a custom script and I have keyboard shortcut for start and stop actions. One thing the script does is that when I "stop" my work, it sets mine IRC nick to "jhutar_afk" and when I "start" my work it sets the nick back to plain "jhutar". This is e.g. handy taking a launch break or going for some errands. The same is possible with Slack:

I have started with How to set a Slack status from other apps article. It is very easy.

  1. Create a legacy token (I know, it is legacy - need to investigate how to use current way :-/) and put it to the variable token='xoxp-0000000000-000000000000-000000000000-00000000000000000000000000000000'
  2. Construct a json to send: profile='{"status_text": "Away from keyboard", "status_emoji": ":tea:"}'
  3. Send that to the API: curl -X POST https://slack.com/api/users.profile.set --silent --data "profile=$profile" --data "token=$token"

This way I can set various statuses. Then I have realized that what I really want is to set mine presence (that green or empty/black dot next to your nick). That is easy as well:

  1. Again (see above), prepare your token token='xoxp-0000000000-000000000000-000000000000-00000000000000000000000000000000'
  2. Use the Slack API: curl -X POST https://slack.com/api/users.setPresence --silent --data "presence=away" --data "token=$token" (to set yourself away) or use presence=auto (for a normal mode when Slack decides based on your activity)

Given how long I was avoiding to actually add it to my script, it was very easy at the end :-)

2020-02-05

My Prometheus@OpenShift cheat-sheet

Prometheus is monitoring solution in OpenShift and I'm reading some basic out of it after some performance tests. Here are the queries I'm using:

Get CPU consumption by pods xyz...:

sum(pod_name:container_cpu_usage:sum{pod_name=~'xyz.*',namespace='qa'})

Now for memory usage (these "POD" and "''" container names seems to be doubling the value):

sum(container_memory_usage_bytes{namespace='qa', pod_name=~'xyz.*', container_name!='POD', container_name!=''})

Also see these nice examples on how to construct query.

To Querying Prometheus via API, I have used range query and this Python code:

assert start is not None and end is not None, \
    "We need timerange to approach Prometheus"

# Get data from Prometheus
token = 'your `oc whoami -t`'
url = 'https://prometheus-k8s.openshift-monitoring.svc:9091/api/v1/query_range'   # I'm running this inside the cluster, so I can use internal hostname
headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json',
}
params = {
    'query': monitoring_query,   # this will be some query from above
    'step': monitoring_step,   # using 60 seconds here
    'start': start.strftime('%s'),
    'end': end.strftime('%s'),
}
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)   # security is hard ;)
response = requests.get(url, headers=headers, params=params, verify=False)

# Check that what we got back seems OK
response.raise_for_status()
json_response = response.json()
assert json_response['status'] == 'success'
assert 'data' in json_response
assert 'result' in json_response['data']
assert len(json_response['data']['result']) == 1
assert 'values' in json_response['data']['result'][0]

data = [float(i[1]) for i in json_response['data']['result'][0]['values']]