Updating a Tag Value via DNS (HowTo)

It's so easy I'm pasting the gateway timer script below. No additional modules are required.

If you have tag-like values in Redis, you can put RKVDNS in front of it and query it via the DNS, no other client or e.g. HTTP service is required in the Ignition installation.

Here's a slightly longer writeup: http://athena.m3047.net/ignition-tags.html

from socket import gethostbyname, inet_aton
from struct import unpack

from com.inductiveautomation.ignition.common.model.values import BasicQualifiedValue, QualityCode

TAG = "RKVDNS_Tag"
RKVDNS_GATEWAY = "redis.sophia.m3047"
REDIS_KEY = "test_x"

try:
    value = unpack('>l',inet_aton(gethostbyname(REDIS_KEY+'.get.'+RKVDNS_GATEWAY)))[0]
    quality = QualityCode.Good
except Exception as e:
    value = system.tag.readBlocking([TAG])[0].value
    quality = QualityCode.Bad
    system.util.getLogger(TAG).error(str(e))
bqv = BasicQualifiedValue(value, quality, system.date.now())
system.tag.writeBlocking([TAG], [bqv])
6 Likes

Thanks for sharing the information, but could you write a few lines to explain its usefulness?

  • What is Redis?
  • Can you give an example?

I don't know anything about you or your use case, or what use case you have in mind. So I'm going to have to tell you what my use cases are.

Before I do that, Redis is a key/value database which is popular for storing telemetry data as well as for caching; for instance it has an "increment" operator. I don't know that "write caching" is a thing, but it is common in telemetry (and logging) to write a lot more than you read. Smells a lot like tags. Coincidentally I read a biography about the guy who created Redis and his father programmed PLCs... just a coincidence I'm sure.

DNS is the grandparent of key/value databases. It is distributed / federated and optimized for reads. DNS and Redis go together like peanut butter and chocolate in my opinion.

We're seeing a lot of "upload everything to the cloud" right now, but how does that assist local observability, where the observable is observed and the telemetry comes from? DNS and Redis are a good fit for that.

Ironically DNS and Redis are used in "the cloud" for this purpose; and DNS in particular is used for configuration management. It's used on a wider scale for control, for example proof of ownership of domains, control information about who can send email for a domain, file signature checking for endpoint detection and response to name a few. So DNS is not just a "telephone book" for web sites. (Although we're abusing that specific functionality to update tags.)

Let's start with the "bare knuckle" use case, configuration information. Obviously the Ignition Gateway leverages DNS for e.g. finding the SQL database. Since gethostbyname() isn't going to allow us to access anything more advanced than 32 bit integers, we can't do a lot of the kinds of things that we could do with a dedicated DNS library. But we can have the DNS provide awareness of things which are outside of the SCADA system: status of the MRP / ERP system, status of the network infrastructure, and so on. It's not onerous to update a DNS zone for those things especially if you use dynamic updates; and these are not (should not) be highly guarded values (they might need integrity guarantees but they don't require secrecy). DNS could also be used as a lookup / validation table (EDR uses it this way for file hashes), for instance for inventory codes, in lieu of a SQL table although that's not exactly the use case for updating a tag which is what we're talking about here. So anyway this is a way for global information which is unlikely to be written to an OPC tag to be picked up and reflected in the tag menagerie: create a DNS A record for it.

So, Redis. Let's say your facility has an automated license plate reader (ALPR). Let's say that's not in the bailiwick of the SCADA system. You might encounter a lot of resistance getting the owner of the ALPR system to write to an OPC server (maybe, maybe not, or it costs a lot of money), but the odds are if it's "cloudy" you'll have an easier time getting them to write the license plates to a Redis database (with expiration TTLs). Since RKVDNS supports the Redis KEYS operator you can get the count of license plates seen in the window determined by the expiration TTL loaded into a tag.

Anyway, it provides a way to bridge the gap between the SCADA environment and the "cloudy" environment. Ignition provides HTTP access to external data (and the SQL datastore is external data for that matter) and DNS is just another, more limited way to access external data using (and abusing) a well known and understood infrastructure-level technology: a controlled "data diode" (readonly) allowing access to some telemetry data which is outside the scope of levels 1 and 2.

If you care about packets, a "clean" DNS request/response (for a small amount of data!) takes just two packets: a UDP request and a UDP response. If you think UDP is truly "unreliable" then you need to work with your switch / network people on traffic shaping. Plus, you use the internet don't you? Seeing problems, much?

I'm an "internet plumber" and I come to Ignition because it came up in small telemetry gig. I worked on SCADA for two paper mills (VAX Pascal!) many years ago. I am a cloud skeptic, I remain convinced local control is paramount. Here's a way to deconflict and find common ground.

2 Likes

Errata:

Yes it does, but what I'm actually talking about here is KLEN which is not a Redis operator but is synthesized by RKVDNS because it's useful. Again, in this specific context everything has to be a 32 bit integer.

Enhancement:

Specifically for the ALPR case (or similar) where presumably there's a common prefix for the keys and in consideration of the gethostbyname() implementation's strong allergy to anything that doesn't look like a hostname, I just added a KPLEN operator to RKVDNS which takes the prefix and appends a wildcard character internally:

>>> unpack('>l',inet_aton(gethostbyname('plates.kplen.redis.athena.m3047')))[0]
13

I think this is really neat, even if it is a bit obscure.

(not that Redis is obscure, but this trick to read keys via dns to avoid additional libraries in Ignition...)

1 Like

This is pretty novel, good stuff.