[Nagiosplug-devel] tip for plugin development and temp files

Andreas Ericsson ae at op5.se
Thu Dec 29 04:38:34 CET 2011

On 12/28/2011 08:44 AM, Matthieu Fournet wrote:
> Hi all,
> I'd like to add my 10 cents to the plugins developer guidelines (at section
> "3.3. Don't make temp files unless absolutely required",
> http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN254),
> regarding the use of temporary files.
> Of course I agree with this piece of advice, but considering there's no
> other solution than creating a temp file (carefully, and cleaning
> everything before leaving !),

There are always other options than creating tempfiles. If you create the
tempfile from the plugin, you will already know its contents at creation
time or you would not be able to fill the tempfile with data.

Since the only thing you'd ever want to do with a tempfile is to read
its contents back in for further processing, the whole step of hitting
the disk(cache) and then grabbing it back is completely useless.

 there's one more rule developers have to keep
> in mind : make temp files name unique.
> Indeed, we all tend to name temp files like "tmp" or "temp", or whatever.
> But in case of concurrent executions (the same plugin is run on the same
> poller for 2 differents hosts, OR 2 distinct plugins running simultaneously
> create temp files), you'll end up with both plugin trying to read / write /
> open / delete the same file, and you may encounter some "file not found"
> errors, whereas while running plugins manually, everything works just fine.
> So my advice would be to name temp files like :
> tmp_[plugin_name]_[timestamp]
> or any combination so that temp files never collide.

While I agree with the general thrust of your advice, I most strongly
disagree with this way of creating tempfiles. There are pre-created
functions for creating temporary files in very nearly all languages I've
ever come across, with raw assembly being the sole exception.

Those schemes are meant for fault-tolerant inter-process communication
though, such as mailservers spooling mails. For very shortlived files
created, used and deleted by the same program, using the process id of
the running process is always a better choice. All languages any sane
man would ever want to use for plugin writing has the ability to get
the process id as well, and it's always built into the core of the
language and thus always available.

But like I said before; There is never a need for tempfiles and those
who opt to use them generally do so because they lack knowledge of the
language they're using.

Andreas Ericsson                   andreas.ericsson at op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.

More information about the Devel mailing list