# Checking NTP state with Python/dbus

**URL:** <https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773>\
**Category:** balenaOS\
**Tags:** pendinguserresponse, summary\
**Created:** [April 30, 2019, 1:58am UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773 "2019-04-30T01:58:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejohnso49](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@ejohnso49](https://forums.balena.io/u/ejohnso49)\
**Post date:** [April 30, 2019, 1:58am UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/1 "2019-04-30T01:58:47Z")

</div>

Continuing the discussion from [Check NTP synchronization status from Python](https://forums.balena.io/t/check-ntp-synchronization-status-from-python/1262/2):

I’m getting an error trying to replicate this code. When I attempt to call the GetAll method, I receive this error:  
`dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name :1.1121 was not provided by any .service files`

I don’t think this error is due to the way the container is setup because I’m able to communicate to systemd via DBus.

Does this have anything to do with the switch from `systemd-timesyncd` to `chrony`? I’d like to stick with Python to get the current state of NTP on the system, but if this method is no longer valid would parsing the output from a `chronyc` command suffice?

Thanks!

---

<div class="post-metadata">

**Author:** ![telphan](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@telphan](https://forums.balena.io/u/telphan)\
**Post date:** [April 30, 2019, 11:13am UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/3 "2019-04-30T11:13:49Z")

</div>

Hi,  
Just make sure we are on the same page, are you running this code?:

```
import python
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.timedate1',
                       '/org/freedesktop/timedate1')
interface = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
properties = interface.GetAll('org.freedesktop.timedate1')
print(properties)

```

Kind regards,  
Theodor

---

<div class="post-metadata">

**Author:** ![ejohnso49](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@ejohnso49](https://forums.balena.io/u/ejohnso49)\
**Post date:** [April 30, 2019, 3:14pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/5 "2019-04-30T15:14:11Z")

</div>

Yep, sorry I should have included that with my question. The code block fails at `interface.GetAll("org.freedesktop.timedate1")` with this exception:

`dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name :1.1121 was not provided by any .service files`

---

<div class="post-metadata">

**Author:** ![sradevski](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/sradevski/32/1609_2.png) [@sradevski](https://forums.balena.io/u/sradevski)\
**Post date:** [April 30, 2019, 8:51pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/6 "2019-04-30T20:51:26Z")

</div>

I tried to reproduce the problem, but it works correctly for me. What I did is I took a python base image, installed `python3-dbus`, ran “export DBUS\_SYSTEM\_BUS\_ADDRESS=unix:path=/host/run/dbus/system\_bus\_socket”, and ran the code you mentioned, and I could see the expected output. What version of python did you try it on?

---

<div class="post-metadata">

**Author:** ![ejohnso49](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@ejohnso49](https://forums.balena.io/u/ejohnso49)\
**Post date:** [April 30, 2019, 9:13pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/8 "2019-04-30T21:13:35Z")

</div>

I ran it using v3.6.8. I’m wondering if it is a problem with the way the image is setup? I have an ENV line to set the system bus address, but I’m wondering if the way I installed `dbus-python` package is bad. I didn’t use the Debian package, I just used `pip` and my requirements file. Let me try with your method, thanks!

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [April 30, 2019, 9:23pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/9 "2019-04-30T21:23:12Z")

</div>

Indeed, having that ENV line in the Dockerfile might not work, depending on how your container starts systemd. If you’re using an old resin/ base image, you might actually be making the container’s systemd init use that socket, which can cause all sorts of problems. So in general we recommend setting the env var only for the processes that need it. And it might look like everything’s okay because you can reach systemd over DBus, but you want to make sure you can reach the _host_’s systemd, and not the container’s.

(I think installing the package with pip is probably fine though)

---

<div class="post-metadata">

**Author:** ![ejohnso49](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@ejohnso49](https://forums.balena.io/u/ejohnso49)\
**Post date:** [April 30, 2019, 9:31pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/10 "2019-04-30T21:31:39Z")

</div>

Thanks for the help!

Here’s my Dockerfile for this container:  
FROM balenalib/raspberrypi3-python:3.6-build

```
WORKDIR /src

ENV INITSYSTEM on
ENV DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

RUN apt-get update && apt-get install -y \
    vim \
    && rm -rf /var/lib/apt/lists/*

COPY ./requirements.txt /src/requirements.txt
RUN pip3 install -r /src/requirements.txt
COPY . /src/

# Add path of common container package
ENV PYTHONPATH="$PYTHONPATH:/var/lib/common_package"

CMD ["python", "monitor.py"]
```

---

<div class="post-metadata">

**Author:** ![ejohnso49](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@ejohnso49](https://forums.balena.io/u/ejohnso49)\
**Post date:** [April 30, 2019, 11:12pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/12 "2019-04-30T23:12:10Z")

</div>

I think specifically installing the `python3-dbus` package fixed the problem I was encountering. I’m able to use the DBus calls through Python now. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [April 30, 2019, 11:13pm UTC](https://forums.balena.io/t/checking-ntp-state-with-python-dbus/6773/13 "2019-04-30T23:13:09Z")

</div>

That’s good to hear!
