I needed some tools for talking to the Known API from the command line in order to play around with a few ideas I’ve been having.

So, I put together a few BASH shell scripts.

Installation

  • Install the prerequisites: curl php_cli python openssl base64
  • Check out the repository and add it to your system path.

Note, due to this bug, you’ll need to be running the latest version of Known if you want to use the syndication functionality.

Talking to Known

The first thing you’ll need (other than a Known account of course) is to get your API key, you can find this in your settings page under “Tools and Apps”.

You can then use those as parameters to known.sh. For example, to make a status update you’d type:

echo "body=my+data" | known.sh https://mysite.com/status/edit *username* *apikey*

Of course, you might want to use one of the wrapper scripts like status.sh, which also supports syndication e.g:

echo "my tweet" | status.sh https://mysite.com *username* *apikey* twitter::username

If successful, the scripts will output a JSON representation of what the API says.

Have fun!

» Visit the project on Github...

5 thoughts on “Post to your Known site from the command line

  1. RT @benwerd: This work paves the way for infrastructure & devices to post to their own Known site. The #indieweb of things? marcus-povey.co.uk/2015/03/26/pos…

  2. RT @benwerd: This work paves the way for infrastructure & devices to post to their own Known site. The #indieweb of things? marcus-povey.co.uk/2015/03/26/pos…

  3. I manage a whole number of device and servers, which are monitored by various utilities, including Nagios. I also have clients who do the same, as well as using other tools that produce notifications – build systems etc.
    Nagios is the thing that tells me when my web server is unavailable, or the database has fallen over, or, more often, when my internet connection dies. I have similar setups in various client networks that I maintain.
    It logs to the system log, sends me emails, and in some urgent cases, sends a ping to my phone. All very handy, but isn’t very handy for other casual users who may just want to see if things are running properly. For those users, who are somewhat non-technical, it’s a bit much to ask them to read logs, and emails often get lost.
    For one of my clients we had a need to be able to collect these status updates from different sources together, make it more persistent, and make it visible in a much more accessible way than log messages (which has a very poor signal to noise ratio) or email alerts (which only go to specific people).
    “Known” issues
    A solution I came up with was to create a Known site for the network which can be used to log these notifications in a user friendly, chronological and searchable form.
    I created an account for my Nagios process, and then, using my Known command line tools, I extended the Nagios script to use my Known site as a notification mechanism.
    In commands.cfg:



    define command {
    command_name host-notify-by-known
    command_line echo “$HOSTNAME$: $HOSTSTATE$” | /etc/nagios/known_nagios_notify.sh
    }
    define command {
    command_name service-notify-by-known
    command_line echo “$HOSTNAME$ – $SERVICEDESC$ : $SERVICESTATE$. Additional info: ‘$SERVICEOUTPUT$'” | /etc/nagios/known_nagios_notify.sh
    }


    12345678

    define command {        command_name hostnotifybyknown        command_line echo “$HOSTNAME$: $HOSTSTATE$” | /etc/nagios/known_nagios_notify.sh}define command {        command_name servicenotifybyknown        command_line echo “$HOSTNAME$ – $SERVICEDESC$ : $SERVICESTATE$. Additional info: ‘$SERVICEOUTPUT$'” | /etc/nagios/known_nagios_notify.sh}


    Then in conf.d/contacts.cfg I extended my “Root” contact:



    define contact{
    contact_name root
    alias Root
    service_notification_period 24×7
    host_notification_period 24×7
    service_notification_options w,u,c,r
    host_notification_options d,r
    service_notification_commands notify-service-by-email, service-notify-by-known
    host_notification_commands notify-host-by-email, host-notify-by-known
    email root@localhost
    }


    1234567891011

    define contact{        contact_name                    root        alias                           Root        service_notification_period     24×7        host_notification_period        24×7        service_notification_options    w,u,c,r        host_notification_options       d,r        service_notification_commands   notifyservicebyemail, servicenotifybyknown        host_notification_commands      notifyhostbyemail, hostnotifybyknown        email                           root@localhost        }


    Finally, the script itself, which serves as a wrapper around the api tools and sets the appropriate path etc:



    #!/bin/bash

    PATH=/path/to/BashKnown:”${PATH}”

    status.sh https://my.status.server nagios *YOURAPICODE* >/dev/null

    exit 0


    1234567

    #!/bin/bash PATH=/path/to/BashKnown:“${PATH}” status.sh https://my.status.server nagios *YOURAPICODE* >/dev/null exit 0


    Consolidating rich logs
    Of course, this is only just the beginning of what’s possible.
    For my client, I’ve already modified their build system to post on successful builds, or build errors, with a link to the appropriate logs. This particular client was already using Known for internal communication, so this improvement was logical.
    The rich content types that Known supports also raises the possibility of richer logging from a number of devices, here’s a few thoughts of some things I’ve got on my list to play with:
    Post an image to the channel when motion is detected by a webcam pointed at the bird feeders (again, trivial to hook up – the software triggers a script when motion is detected, and all I have to do is take the resultant image and CURL it to the API)
    Post an audio message when a voicemail is left (although that’d require me to actually set up asterisk, which has been on my list for a while now)
    Attach debugging info & a core dump to automated test results
    I might get to those at some point, but I guess my point is that APIs are cool.


    Thanks for visiting! If you’re new here you might like to read a bit about me.
    (Psst… I am also available to hire! Find out more…)


    Follow @mapkyca
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?’http’:’https’;if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+’://platform.twitter.com/widgets.js’;fjs.parentNode.insertBefore(js,fjs);}}(document, ‘script’, ‘twitter-wjs’);


    Share this:EmailLinkedInTwitterGoogleFacebookReddit

  4. A little while ago I introduced you to some command line tools for talking to your Known site.
    So, using this, I put together a very quick hack that lets you tweet, via your known site, from the Unix command line.
    Because, that’s how I roll.
    In .bashrc I added the following:



    alias tweet=’status.sh https://mysite.com USERNAME APIKEY twitter::TWITTERUSER’


    1

    alias tweet=‘status.sh https://mysite.com USERNAME APIKEY twitter::TWITTERUSER’


    Assuming status.sh is on your path, when you type tweet it’ll let you write a single line of text which will be sent to your Known site and syndicated elsewhere.
    Note, due to this bug, you’ll need to be running the latest version of Known for this to work!


    Thanks for visiting! If you’re new here you might like to read a bit about me.
    (Psst… I am also available to hire! Find out more…)


    Follow @mapkyca
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?’http’:’https’;if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+’://platform.twitter.com/widgets.js’;fjs.parentNode.insertBefore(js,fjs);}}(document, ‘script’, ‘twitter-wjs’);


    Share this:EmailLinkedInTwitterGoogleFacebookReddit

Leave a Reply