The shiny open source world

in #tech7 years ago (edited)

As you may know, we are big fans of open source and the cloud, because they have some design principles in common. One of them is Separation of Concerns, which in short means do only one thing, but do it good. The interesting part now is orchestration. In the open source world orchestration works quite good, if all projects that need to be tied together have well defined and standardized interfaces.

In one of our current use cases we need to tie together nextcloud, grafana and slack. Ok, slack is not part of the open source ecosystem, but at least has well defined interfaces :)

We are scraping a lot of metrics via prometheus and display them in grafana. Grafana is not only our dashboard, but also our alert headquarter and should inform us, if some metrics are going crazy. The notification to slack is easily set up. But we also wanted to use the image rendering feature of the alerting metric. Therefor we need a storage obviously. Grafana supports S3 compatible and webDAV storages. There a lot of projects or hosting providers for both standards, but we are also having other software in place, which already uses webDAV, so we went with webDAV and decided to give nextcloud a try - the last part in the notification puzzle.

Setting up the storage is easy and uploading the pictures from grafana to nextcloud was easy. According to Murphys Law there is always a pitfall, otherwise this post would be much shorter :)

So, the pictures were stored successfully, but they need to be reachable publically. With nextcloud it is possible to create public links on specific folders, nohting special, but quite neat. But the url needs to point directly to the picture, not the nextcloud ui. Nextcloud supports this somehow with its previewapi and one can build a link like this:

[fqdn]/index.php/apps/files_sharing/ajax/publicpreview.php?x=1400&y=1400&a=1&t=[token]&file=[fileName]

x -> maxWidth
y -> maxHeight
a -> 1 means the aspect ratio of the image is preserved
token -> the sharing token created by nextcloud
file -> obviously the name of the file ;)

But this doesn't work with grafana. Grafana has the possibility to define a public url for public access. This is the right starting point for the solution. Unfortunately it appends the filename to the path and we have no control over it. This is why we proposed the use of placeholders to grafana in order to have more control about the shape of the ui.

Of course we cannot wait to use pictures in our slack alerting, as this features is just to cool. So we decided to use a fake public url and rewrite it for nextcloud. We defined the public url like this:

[fqdn]/publicpreview/[fileName]?x=1400&y=1400&a=1&t=[token]

Since there is no index.php prepended it is unlikely that it might conflict with existing nextcloud routes.

The second part are the rewrite rule. For a current version of haproxy (> 1.5) this looks like this:

backend nextcloud
  acl is_grafana_request path_beg -i /publicpreview/
  http-request set-query %[query]&file=%[path,regsub(/publicpreview/,)] if   is_grafana_request
  http-request set-path /index.php/apps/files_sharing/ajax/publicpreview.php if is_grafana_request

Ét voila! We have pictures, yay!

Let us know, if this snippet has helped you mastering the daily it devil.

Image credit: https://unsplash.com/@arstyy