[Plugin] Unraid JSON API


Fma965

Recommended Posts

Introduction

 

I recently modified the Corsair PSU plugin and made it easier to add to applications that can use REST such as Home Assistant, this got me thinking what i if i wrap the ini files used by emhttp in to JSON so i can get WAY WAY more statistics in Home Assistant from Unraid and so this plugin was created. super simple at only about 6 lines of PHP code.

https://community.home-assistant.io/t/unraid-json-api/158652

 

This plugin is a wrapper for reading the files used by Unraid / emhttpd for displaying various statistics, the files are read from /var/local/emhttp/*.ini and are converted to JSON for easy access by services/applications like Home Assistant

 

This plugin has no 'frontend' and is only accessible directly via http://TOWER/plugins/jsonapi/api.php?file=XXX.ini for example to get the disks.ini file you would do  http://TOWER/plugins/jsonapi/api.php?file=disks.ini

 

currently the following files exist that can be used but it's dynamic so if new files are added in new versions of Unraid in the same folder then these will be usable also.

 

  • cpuload.ini
  • devs.ini
  • diskload.ini
  • disks.ini
  • monitor.ini
  • network.ini
  • sec.ini
  • sec_afp.ini
  • sec_nfs.ini
  • shares.ini
  • users.ini
  • var.ini


24Lb3xZ.png

 

Usage

Plugins > Install Plugin

https://raw.githubusercontent.com/Cyanlabs/jsonapi-unraid/master/jsonapi.plg

 

Source: https://github.com/Cyanlabs/jsonapi-unraid

 

I will add some Home Assistant examples once i determine the best way.

 

NOTE: please note that this currently is a raw json output, a custom HA component would be the best way to access this data but it is possible without a custom component (HA has a 255 character REST sensor state limit)

  - platform: rest
    name: Unraid
    resource: "http://192.168.1.3:965/plugins/jsonapi/api.php?file=disks.ini"
    username: root
    json_attributes:
      - disk1
      - disk2
    password: !secret unraid_password
    authentication: basic
    value_template: '{{ value_json["disk1"]["status"] }}'
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json

despite it not being able to do more than 255 you can work around it with the attributes, the above code will show something like this

XTaCcCd.png

and you can access specific values with like this

4ByyOf4.png

 

My python isn't great but i believe this is a good starting point - https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/pvoutput/sensor.py

 

Things like CPU load are easy though

sensor:
  - platform: rest
    name: Unraid CPU
    resource: "http://192.168.1.3:965/plugins/jsonapi/api.php?file=cpuload.ini"
    username: root 
    password: !secret unraid_password
    authentication: basic
    value_template: '{{ value_json["cpu"]["host"] }}'
    headers:
      User-Agent: Home Assistant
      Content-Type: application/json

 

@LintHart thanks for the idea of integrating the PSU statistics in to Home Assistant and therefore making this an idea for me to create :)

Edited by Fma965
  • Like 1
  • Thanks 1
Link to comment

Just to clarify, due to limitations afaik in the Unraid plugin system this does require basic auth when accessing the json. example snippet of HA code would be this

  - platform: rest
    resource: "http://TOWER/plugins/jsonapi/api.php?file=XXX.ini"
    username: root
    password: !secret unraid_password
    authentication: basic

 

Link to comment
1 minute ago, melagodo said:

Thanks so much for this, you're the man!!

I'll try to improve my ninja templating skills to show % disk free and post back if I end up with something.

Thanks again, I was waiting for this :)

Thanks, my friend is going to look in to a companion custom home assistant component but he is super busy currently so not sure when he will get it done. In the meantime the workaround sensor attributes works. It's possible there are other files used by unraid that might be usable in different folders so I'm happy to add others if they are relevant.

  • Like 1
Link to comment

Please note this does not work on unraid 6.8 due to the removal of basic auth and the change to form authentication. There are a few workaround options for this but really limetech should consider a API system or key in this day and age. Basically every other application out there has one now a days.

Link to comment
25 minutes ago, Fma965 said:

Please note this does not work on unraid 6.8 due to the removal of basic auth and the change to form authentication. There are a few workaround options for this but really limetech should consider a API system or key in this day and age. Basically every other application out there has one now a days.

Setting this app to be incompatible with 6.8 until this gets sorted out.   Let me know...

Link to comment
1 minute ago, Squid said:

Setting this app to be incompatible with 6.8 until this gets sorted out.   Let me know...

No worries, there are a few workaround options but they are clunky and unnecessary, really some sort of programmatic login should be added to unraid. Whether it's a simple ?apikey= or something more complicated. Either way me and my friend will see what workaround we come up with. 

Link to comment
9 minutes ago, LintHart said:

Well this is very exciting thank you! Also check out the new unraid-api docker by electricbrainUk. I’ve been helping them test it for last couple of weeks and it’s awesome for some docker/vm switches and statistics in home assistant. Now to check this out :) 

Yes i saw that, haven't looked in to it yet, it uses MQTT from my understanding, i already have a MQTT broker so thats simple. Please note this plugin is going to break in 6.8 but i have looked through the nginx configuration files in and can see references to a node REST api at /api, it doesn't currently work but it shows limetech / dynamix is working on a API

Link to comment

Upon further inspection of the code i can see there is already work in progress code for an API in 6.8 via a plugin called unraid.net so this kinda makes my program redundant. 

 

ZBVO3FH.png

 

 in the /boot/config/plugins/dynamix/dynamix.cfg file you have this

[remote]
apikey="bbff6d7320a3cfa74964c58b5c5d3b0XXXXXXXXXXXXXXXXXXXXXXe2bc79518f2e4f0c4"
wanaccess="no"
wanport="0"
{
   "data": {
       "vars": {
           "version": "6.8.0",
           "port": 965
       },
       "disks": [
           {
               "device": "/dev/sda",
               "type": "HD",
               "name": "Extreme"
           },
           {
               "device": "/dev/sdb",
               "type": "SSD",
               "name": "MTFDDAK256MAM-1K12"
           },
           {
               "device": "/dev/sdc",
               "type": "SSD",
               "name": "Crucial_CT250MX200SSD4"
           },
           {
               "device": "/dev/sdd",
               "type": "HD",
               "name": "ST2000LM007-1R8174"
           },
           {
               "device": "/dev/sde",
               "type": "SSD",
               "name": "LITEON_LCH-256V2S-HP"
           },
           {
               "device": "/dev/sdf",
               "type": "HD",
               "name": "WDC_WD5000AAKX-60U6AA0"
           },
           {
               "device": "/dev/sdg",
               "type": "HD",
               "name": "WDC_WD20EZRZ-00Z5HB0"
           },
           {
               "device": "/dev/sdh",
               "type": "SSD",
               "name": "SAMSUNG_MZ7PD256HCGM-000H7"
           },
           {
               "device": "/dev/sdi",
               "type": "HD",
               "name": "WDC_WD20EZRZ-00Z5HB0"
           },
           {
               "device": "/dev/sdj",
               "type": "HD",
               "name": "WDC_WD20EZRZ-00Z5HB0"
           },
           {
               "device": "/dev/sdk",
               "type": "SSD",
               "name": "MTFDDAK256MAM-1K12"
           },
           {
               "device": "/dev/sdl",
               "type": "HD",
               "name": "ST2000DM008-2FR102"
           },
           {
               "device": "/dev/nvme0n1",
               "type": "NVMe",
               "name": "Samsung_SSD_960_EVO_250GB"
           }
       ]
   }
}

looks like Unraid API is definitely a thing in 6.8 and therefore completely makes this quick plugin redudant.

Edited by Fma965
Link to comment
  • 2 weeks later...
2 minutes ago, ezra said:

Ive just checked out the github repo, it gives me some more insights. Indeed you gave me info already, discovered this thread later. Thanks

Yeah this is a super simple conversion of the ini files that unraid / dynamix use and converts them to JSON just for external access.

 

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.