Disable chrony NTP server when unsync - openwrt

I have a OpenWRT router running chrony (chronyd). The hardware doesn't have RTC, so after a power loss, the system clock goes crazy.
I want to have a LAN NTP server, but if somehow have no internet connection and the clock is wrong because a power loss, the NTP server passes the wrong time to the clients. Consider I'm using 'cheap' NTP clients (Android/Arduino/other routers/cameras) that have no way to prevent a big clock slew.
I would like to disable chrony NTP server on reboot and wait to a first sync to enable it. I thought this was a common setting, but couldn't find anywhere.
I've also tried ntpd and I'm open to alternatives (but it must run on OpenWRT).
Note: I'm already trying to use a USB GPS as a more reliable clock source even without internet connection.

My solution so far:
On chrony.conf, set to be disabled on startup.
deny all
Then, I created a script to check every minute if clock is sync (using chronyc) and then enable it again.
until ! chronyc tracking | grep "Reference ID" | grep -q "()"
do sleep 60
done
chronyc allow all
Finally, set to run script on startup.
Seems this in only possible with chrony (not ntpd), as it have a client to change settings on the fly.

Related

How to measure elapsed time, including across device reboots and without regard for possibly-manually-changed system time?

What is the best way to measure the elapsed time between any two points, including across device reboot? I thought clock_gettime_nsec_np(CLOCK_MONOTONIC) would work but I recently realized that the clock restarts when the device reboots.
In my understanding different solutions based on system clock time would change if the user changes the device time, which is an issue.
If you have a reasonable guarantee of network connectivity for your application, you could leverage NTP with an NTP client like instacart/TrueTime.swift to get the current time from an NTP server you control or trust at the start and end of the time span you wish to measure.
Keep in mind that NTP (at its core) is rather vulnerable to MiTM attacks by motivated attackers (or outright firewall blocking of its UDP port 123 by heavy-handed system administrators), so the acceptability of this as a solution will depend heavily on your threat model.

looking for alternative solution than IPFW for slowing down an internet connection

I need to slow down (simulate bad) internet connection, I found some documentation where it was achieved by "ipfw pipe" command , the thing is that in latest MAC OS versions , ipfw was deprecated (and removed)...
I was wondering if there are any alternative to the ipfw API ?
Does anyone know how latest Network Link Conditioner achieves it?
The original previous way which enabled to slow down an internet connection :
sudo ipfw pipe 1 config bw 56Kbit/s delay 200 plr 0.2
and to clear the pipe :
sudo ipfw delete 1
Thanks.
I don't know much about Mac OS (I use Linux myself), but I'll give this a shot.
A bunch of digging established that ipfw seems unavailable, as you say.
I was also unable to find a way to use the Network Link Conditioner from the command line. Everything should be usable from the command line, so that's stupid.
One work around would be to try to access the NLC from within AppleScript. The following will get you started on toggling the NLC:
property thePane : "com.apple.Network-Link-Conditioner"
tell application "System Preferences"
activate
set the current pane to pane id thePane
--delay 2
end tell
---
tell application "System Events"
tell application process "System Preferences"
try
click ((checkboxes of window "Network Link Conditioner") whose description is "enable switch")
on error
click ((checkboxes of window "Network Link Conditioner") whose description is "enable switch")
end try
end tell
end tell
I think that you can run a script from the terminal with osascript <SCRIPT>.
As an alternative, Charles Proxy is a pay-to-use program that can be used to perform throttling, provided you can convince the software you are testing to connect to the proxy's port rather than directly to the internet. Maybe there are free proxy solutions out there somewhere?
Perhaps Squid would work in that regard. SquidMan seems to be an easy-ish way to install it for Mac. It looks as though DelayPools and or Client Bandwidth Limits might be useful for simulating a low speed connection, though I can't find evidence of people having used them for such.
I found several solutions that might work. They come from some old threads, but they might help:
How to simulate slow internet connections on the mac
Apple has made a very handy official tool to slow down the network connections on you Mac for testing purposes.
The Network Link Conditioner preference is a free download from within Xcode (for Lion and later OS). Additionally, iOS has similar function accessible from within Xcode and iOS 6 or later.
How do I simulate a bad Wi-Fi connection on my iPad?
There are a few ways you can do this, depending on your situation:
Move further away from your router. While this may seem a bit obvious, I realize that it isn't always possible while testing/debugging (for example, if you are working on a desktop computer).
Put aluminum foil around the router and/or antenna. This will (partially) block some or all of the radio signals by creating a makeshift Faraday cage. The results you get will depend on the strength of your router signal, distance from the router, and other environmental factors.
Set your router's wireless signal power to a lower setting. The method for doing this is different for each router, so you will have to look at the user guide for instructions on how to do this.
Slow down internet for iOS simulator
You can set the network to slow characteristic by testing on devices. Go to Settings -> Developer Settings -> Network Link Conditioner -> Enable. This is for iPhone/iPad running iOS 6
I don't know if the last one you can still do.
I've used many methods in the past for slowing down network connections, among them:
performing a parallel download of some massive Linux ISO file;
physically pulling out the Ethernet cable (at one point, I actually toyed with the idea of building a push-button device that would sit between two cat5 cables and do this without having to physically disconnect the whole cable).
using ifconfig eth0 down ; sleep 1 ; ifconfig eth0 up.
Hopefully one or more of those methods will help.
If you're looking to slow down your network connection because you're doing testing/profiling work, one option is to get a specific device that can create network latency/noise.
For example Apposite Tech's mini2 WAN emulator allows you to change values for bandwidth and packet loss. You can roll your own device too using something like: http://wanem.sourceforge.net/ . It just depends on your needs, time and budget.

Instantly Shut Down/Sleep shortcut

How do I create a shortcut that instantly shuts down the computer in the same way the computer instantly shuts down when holding my power button for 4 seconds. This is for PC.
I tried creating a shortcut using shutdown /s /t 0 but it does not shut down instantly.
thanks.
You can't do that. That's a hardware feature (cuts power to the machine, doesn't really 'shut down').
While its not quite possible to achieve what you described as it is a hardware feature, I believe you can mimic the behavior by sending Emergency Shutdown request to system. The result would be immediate session shutdown which is comparable in time with holding the power button.
In Windows you can achieve this by using NtShutdownSystem and NtSetSystemPowerState.
There is a very nice article about this topic:
http://www.codeproject.com/Articles/34194/Performing-emergency-shutdowns
in Unix/Posix based system you should be able to do this by using:
echo 1 > /proc/sys/kernel/sysrq-trigger ;
echo o > /proc/sysrq-trigger
however you may need to obtain required privileges.

Monitoring Remote Folder over SFTP in a Rails App

As the title says, I need to monitor a remote folder for new files through a SFTP connection.
I setup a daemon process that opens a connection and if it finds any files then it retrieves their contents and if it doesn't then it sleeps for 5 seconds. This works fine it just hovers around 4% CPU usage. Is there a better way to do this and is it bad to keep a connection like this open indefinitely?
That's probably the best thing you can do, given the circumstances. When watched directories get bigger, your daemon will likely run slower and consume more resources.
A single persistent connection is nothing, both on client and server. But if there are many clients, server may slow down.
If you control the other server, a much better way would be to install a daemon on it. Local process can listen to filesystem notifications and broadcast to connected watchers.

How to check that systemtime on web server is configured OR not?

How do I check that system time is configured OR not correctly on the web host server? I need to check its sync with twitter server time. How do I do that? I checked by running "strftime('%c')" on web server that its time is either -7 hours OR +5 different hours my systemtime.
EDIT
OK, I checked time zone as Piskvor mentioned & Web server has 'UTC/GMT -5 hours' while my system is in 'UTC/GMT +2 hours'. my problem is that I need to sure the clock on the server is properly synced with NTP. How do I do that ?
I am using Windows XP.
ntpq -p
You should see a list of your configured clock sources, with "*" indicating a clock source selected as the source for synchronisation.

Resources