News: This forum is now permanently frozen.
Pages: [1] 2 3
Topic: Scheduler extension "Crön"  (Read 11517 times)
« on: January 25, 2012, 01:50:41 »
Lennart Grahl ***
Posts: 153

I have written an extension called "Crön" for m0n0wall which now runs on m0n0wall version 1.8.0b484 and wanted to show it to you guys. I could not find other extensions so I decided to start a new topic in this subforum.

The extension is a scheduler and as far as I know has been requested many times but has not been implemented into m0n0wall yet.
Why a scheduler: Many German ISPs disconnect your internet connection after 24 hours and many routers support to "move" this disconnect to a time where it does not disturb the users but sadly m0n0wall does not provide such a feature. Because of this I decided to write an extension that is among other things capable of "moving" the 24h disconnect.

What the extension is capable of:
- daily, weekly, monthly and one-time-only schedules
- rebooting m0n0wall and restarting the wan interface (dhcp-renew, restart of ppp connection, ...)

In the near future I want to add the possibility to enable and disable wireless. Another idea was to "ping" a host but I am really open to suggestions.
In fact I would really like if you would tell me what features you would like to see included into this extension.

Although the scheduler already works the webinterface extension is just an example how it will probably look when I have finished writing the php-files.

A few screenshots:
http://www.abload.de/img/sysloghipiy.png
http://www.abload.de/img/croenwebinterface1tfqtm.png
http://www.abload.de/img/croenwebinterface2d9qx4.png

Because m0n0wall deletes all the scripts after a reboot this extension would probably need to be compiled into a new m0n0wall build. I have no idea how to do such a thing because I'm a FreeBSD noob. I have read that Manuel won't give any support for extensions so if you are interested in this extension and you do know how to sort this out please reply to this thread.
« Reply #1 on: January 25, 2012, 03:23:53 »
dr01 **
Posts: 79

Do you have the BSD knowledge to add the NO IP feature to the latest version also? See my related post regarding NO IP where it allows static IP clients to still get the captive portal page and access the internet. This feature is really needed in monowall....if you could accomplish this that would be the best added feature so far....let me know...thank you...
« Reply #2 on: January 25, 2012, 13:25:19 »
Lennart Grahl ***
Posts: 153

Do you have the BSD knowledge to add the NO IP feature to the latest version also?

I'm afraid I can't help you in this matter. I have C and PHP knowledge but sadly no real BSD knowledge.
« Reply #3 on: January 26, 2012, 12:00:15 »
Manuel Kasper
Administrator
*****
Posts: 364

I have written an extension called "Crön" for m0n0wall which now runs on m0n0wall version 1.8.0b484 and wanted to show it to you guys. I could not find other extensions so I decided to start a new topic in this subforum.

Looks great - good job! I would like to integrate this into m0n0wall 1.8.x if that's OK with you; not just as an extension, but as an integral part (perhaps under the "Services" section). Assuming that you agree with this, could you please post the code somewhere?

Regards,

Manuel
« Reply #4 on: January 26, 2012, 20:54:15 »
Lennart Grahl ***
Posts: 153

Looks great - good job! I would like to integrate this into m0n0wall 1.8.x if that's OK with you; not just as an extension, but as an integral part (perhaps under the "Services" section). Assuming that you agree with this, could you please post the code somewhere?

Regards,

Manuel

I'd really appreciate that! I've been a bit busy the last two days so I wasn't able to continue the work. How would you like to proceed?

The php files for adding/deleting/editing "jobs" aren't ready yet and I'm still using placeholders for the configuration because I don't have a configuration file for now. So my idea would be that I take a look at your function for writing/reading the xml configuration file and try to use it. Then I'll finish the php files and let you integrate them. In this state the php files would require a lot of work from you that I am willing to do.
Already done. As stated above I've used your function to write into the xml configuration file. Now it's just about adding the jobs. I'll probably do that tomorrow.
However, there is one thing I'd like to skip for now which is implementing the enable/disable wireless feature because I guess this isn't as easy to write as a "reboot" feature and I'd rather finish the webinterface scripts. Maybe you have a suggestion how to implement this feature easily.

Something that probably won't change anymore is the croen daemon which basically is a modified version of minicron.c. Here it is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

/* usage: croen 1:job 2:interval 3:pidfile 4:cmd 5:silent */

int main(int argc, char *argv[]) {

/* not enough arguments? exit! */
if (argc < 6)
exit(1);

/* define vars */
int interval, silent;
FILE *pidfd;
interval = atoi(argv[2]);
silent = atoi(argv[5]);

/* interval 0? exit! */
if (interval == 0)
exit(1);

/* unset loads of CGI environment variables */
unsetenv("CONTENT_TYPE"); unsetenv("GATEWAY_INTERFACE");
unsetenv("REMOTE_USER"); unsetenv("REMOTE_ADDR");
unsetenv("AUTH_TYPE"); unsetenv("SCRIPT_FILENAME");
unsetenv("CONTENT_LENGTH"); unsetenv("HTTP_USER_AGENT");
unsetenv("HTTP_HOST"); unsetenv("SERVER_SOFTWARE");
unsetenv("HTTP_REFERER"); unsetenv("SERVER_PROTOCOL");
unsetenv("REQUEST_METHOD"); unsetenv("SERVER_PORT");
unsetenv("SCRIPT_NAME"); unsetenv("SERVER_NAME");

/* go into background */
if (daemon(0, 0) == -1)
exit(1);

/* write PID to file */
pidfd = fopen(argv[3], "w");
if (pidfd) {
fprintf(pidfd, "%d\n", getpid());
fclose(pidfd);
}

/* wait... */
sleep(interval);

/* write syslog message if required */
if (!silent) {
openlog("croen daemon", 0, LOG_NOTICE);
syslog(LOG_NOTICE, "Job %s executed", argv[1]);
closelog();
}

/* remove PID file */
remove(argv[3]);

/* execute */
char command[100];
sprintf(command, "/usr/local/bin/php %s", argv[4]);
system(command);
}

And don't worry, the script will not just calculate the seconds to the next schedule and run the daemon.

Edit: See strikethrough lines.
« Last Edit: January 27, 2012, 05:11:18 by MasterKeule »
« Reply #5 on: January 28, 2012, 14:49:01 »
Lennart Grahl ***
Posts: 153

The Javascript stuff took longer than I anticipated and the validation of inputs took forever (well, what a surprise  Wink) but now it's ready!

New screens:
http://www.abload.de/img/croenwebinterface3ikbdi.png
http://www.abload.de/img/croenwebinterface448xam.png

I've tested my scripts thoroughly and they seem to work fine.

Here is a list to determine which file goes where:
/bin/croen -> /usr/local/bin/croen
/ext/* -> /usr/local/www/ext/croen
/www/* -> /usr/local/www

Apart from these files I have modified the xmlparse.inc (included "job" into $listtags) and added the croen default values into the config.xml:
Code:
<croen>
<interval>60</interval>
<!--
<enable/>
<job>
<name>reboot|restart_wan</name>
<repeat>once|daily|weekly|monthly</repeat>
<syslog/>
*and*
<time>hh:mm</time>
<date>mm/dd/YYYY</date>
*or*
<time>hh:mm</time>
<weekday>number (1-7)</date>
*or*
<time>hh:mm</time>
<day>number (1-28)</date>
</job>
-->
</croen>

If anyone apart from Manuel wants to give it a try:
  • 1. Make a backup of your configuration!
  • 2. Upload the files onto your running m0n0wall.
  • 3. Execute this command: "rm /tmp/croen.c; chmod a+rx /tmp/*; mkdir ./ext/croen; mv /tmp/*.php .; mv /tmp/ext_croen.inc .; mv /tmp/croen /usr/local/bin; mv /tmp/* ./ext/croen; mv ./ext/croen/*.cache /tmp"
  • 4. Edit xmlparse.inc: Search for "roll active secondary wlan" and replace it with "roll active secondary wlan job". <- This is important! If you don't do it you will screw up your config and therefore can't access the webinterface anymore!
  • 5. Look for "Crön" in your Webinterface.

Be aware that you have to restore your configuration before you reboot! So for now use only the job type "Reconnect"!
Explanation: A reboot will reset all files (which includes the xmlparse.inc) but the configuration file. Basically the xmlparse.inc will misinterpret "job" as not being a list and screw up your config.

Edit: Corrected a slight error (rev 2).

Obsolete now. Have a look at the newest 1.8b image.
« Last Edit: May 25, 2012, 23:31:02 by Lennart Grahl »
« Reply #6 on: January 30, 2012, 14:15:26 »
Manuel Kasper
Administrator
*****
Posts: 364

Lennart,

thank you! I've modified the extension to become an integral part of m0n0wall, and committed it to the repository (r492). It appears to work fine for me, but I haven't tested all the possible combinations yet. A new snapshot should be available in 15 minutes or so.

I've changed the name in the GUI to simply say "Scheduler" instead of "Crön", in keeping with the tradition of keeping m0n0wall technology neutral as far as the user is concerned (e.g. the user shouldn't need to care whether the DNS forwarding is provided by Dnsmasq or some other package). I hope that's OK with you Wink

Now I'm wondering whether we're not actually using more memory etc. by having a separate process for each scheduled job than if we simply included the stock FreeBSD cron with a dynamically generated crontab, but for now this will work just fine and most users will probably only have one job (= reconnect WAN interface) anyway.

Again, thank you very much for your contribution!

Manuel
« Reply #7 on: January 30, 2012, 15:03:15 »
Fred Grayson *****
Posts: 994

I just now upgraded to r492, and verified the upgrade took.

It's early and I haven't had my coffee yet, so I am having trouble finding  "Scheduler" in the WebGUI.

Can someone point it out to me?

--
Google is your friend and Bob's your uncle.
« Reply #8 on: January 30, 2012, 15:48:39 »
Lennart Grahl ***
Posts: 153

Lennart,

thank you! I've modified the extension to become an integral part of m0n0wall, and committed it to the repository (r492). It appears to work fine for me, but I haven't tested all the possible combinations yet. A new snapshot should be available in 15 minutes or so.
Thanks! I am really happy to contribute to m0n0wall. To be honest I was a bit surprised by your intention to integrate it into m0n0wall because I haven't seen many contributions to m0n0wall for quite some time (or maybe I have just missed them).

I've changed the name in the GUI to simply say "Scheduler" instead of "Crön", in keeping with the tradition of keeping m0n0wall technology neutral as far as the user is concerned (e.g. the user shouldn't need to care whether the DNS forwarding is provided by Dnsmasq or some other package). I hope that's OK with you Wink
I'm fine with that. It was just a fancy name for the extension. Wink

Now I'm wondering whether we're not actually using more memory etc. by having a separate process for each scheduled job than if we simply included the stock FreeBSD cron with a dynamically generated crontab, but for now this will work just fine and most users will probably only have one job (= reconnect WAN interface) anyway.

Again, thank you very much for your contribution!

Manuel
Mhh, I haven't really looked at crontab because I thought it would be a no-go.
However, I don't think it will be a problem unless someone creates an enormous amount of jobs and even then it will only matter if he has set a large interval. I also think the daemon could be rewritten to schedule multiple jobs and just add them to a "list" if another job has been scheduled. That would fix the memory issue. I will have a look at it sometime.

I would like to add a few more "jobs" in the future if you are ok with it. First of all "Enable/Disable Wireless" because some people like to disable wireless during the night. I was also thinking of adding some sort of "Ping <address>" but I can't think of anyone having a benefit from this. "Enable/Disable Traffic Shaping" and "Change downstream/upstream of Traffic Shaper" was another idea. The reason: Many ISPs (at least in the UK) limit the upstream/downstream at a specific time of the day and the traffic shaping won't work during this time. And of course "Apply Firewall Rule(set)" that has been asked for in the forum but I believe this will be a hard one.
Just reading these lines again I already have many, many more ideas.

Regards,
Lennart
« Reply #9 on: January 30, 2012, 15:51:44 »
Lennart Grahl ***
Posts: 153

I just now upgraded to r492, and verified the upgrade took.

It's early and I haven't had my coffee yet, so I am having trouble finding  "Scheduler" in the WebGUI.

Can someone point it out to me?

Should be in the menu Services -> Scheduler at least the modified fbegin.inc points to it. I'm just upgrading to the new image right now.
« Reply #10 on: January 30, 2012, 16:13:18 »
Fred Grayson *****
Posts: 994

Not in my copy of r492. I can't upload a screen capture to the forum, so I posted one here:

http://www.dslreports.com/getfile/PCy/527ddb.jpg

--
Google is your friend and Bob's your uncle.
« Reply #11 on: January 30, 2012, 16:18:21 »
Lennart Grahl ***
Posts: 153

Not in my copy of r492. I can't upload a screen capture to the forum, so I posted one here:

http://www.dslreports.com/getfile/PCy/527ddb.jpg

Can you access this page manually? http://<m0n0_ip>/services_croen.php
However, your menu looks entirely different compared to mine: http://www.abload.de/image.php?img=menuguj0q.png
« Reply #12 on: January 30, 2012, 16:26:53 »
Fred Grayson *****
Posts: 994

I have it sorted now. It seems that I had to deliberately enable access to it via

/system_groupmanager.php

--
Google is your friend and Bob's your uncle.
« Reply #13 on: January 30, 2012, 16:38:12 »
Lennart Grahl ***
Posts: 153

I have it sorted now. It seems that I had to deliberately enable access to it via

/system_groupmanager.php

Mhh, I haven't thought of the possibility that you aren't logged in as admin. Yeah you have to enable access to them because they are new pages to the user manager.

Edit: By the way. I've just seen that the dhclient is spamming the syslog at reconnect.
« Last Edit: January 30, 2012, 16:46:15 by MasterKeule »
« Reply #14 on: January 30, 2012, 16:58:26 »
Fred Grayson *****
Posts: 994

Yes, I'm not logged in as admin. I changed that long ago, don't remember why I did that, and more importantly I can't seem to figure out how to undo that.

--
Google is your friend and Bob's your uncle.
 
Pages: [1] 2 3
 
 
Powered by SMF 1.1.20 | SMF © 2013, Simple Machines