Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 578985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:19:06+00:00 2026-05-13T14:19:06+00:00

i need some advice and help with a plugin i am writing for Nagios.

  • 0

i need some advice and help with a plugin i am writing for Nagios.

i am writing the plugin in C, but have no previous experience in the language except for minor amounts whilst trying to get this plugin to work.

basically what i am trying to-do is the following.

read a text file that is generated on a remote PC by an application i have written, this program writes nothing more than 5 characters into the file, the first 4 chars are the time in 24 hour format. e.g. 22:30 > 10:30pm

it then needs to take these 4 characters convert them into a time and compare it to the current system time (if there is a difference of 5 mins then it generates a reply to nagios to flag a warning).

I have tried many different ways of doing this, my first attempt was to convert the characters into an integer, then convert the time into an integer and compare the difference .. failed at doing this.

my second attempt is to generate two Time Structs one with the current time in and the other with my “homemade” time in and compare them but this is not working either.

heres my code, no matter what i try the date from the file is always the same as the current system time, i know its something to-do with having to set the time at the top.

t_of_file = time(NULL);
time_from_file = localtime(&t_of_file);

but if i do not do this i get a segmentation fault.

heres the code.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define COPYMODE 0644

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


    struct tm *time_from_file;
    struct tm *the_system_time;

    time_t t_of_file;
    time_t t_of_sys;

    t_of_sys = time(NULL);
    the_system_time = localtime(&t_of_sys);

    t_of_file = time(NULL);
    time_from_file = localtime(&t_of_file);



    time_from_file->tm_year = the_system_time->tm_year;
    time_from_file->tm_mon  = the_system_time->tm_mon;
    time_from_file->tm_mday = the_system_time->tm_mday;
    time_from_file->tm_hour = 10; //should be read in from file
    time_from_file->tm_min  = 30; //should be read in from file
    time_from_file->tm_sec  = the_system_time->tm_sec;
    time_from_file->tm_isdst = the_system_time->tm_isdst;

    t_of_file = mktime(time_from_file);
    printf("%s\n",ctime(&t_of_file));

    t_of_sys = mktime(the_system_time);
    printf("%s\n",ctime(&t_of_sys));

    double difference = difftime(t_of_file, t_of_sys );

    printf("%lf\n",(double)t_of_file);
    printf("%lf\n",(double)t_of_sys);

    if (difference >= 0.0) { //this should be 5 mins, not quite sure what to put here yet
        // second is later than first
        printf("later\n");
    }
    else if (difference < 0.0) {
        // second is earlier than first
        printf("earlier\n");
    }
    printf("%lf\n", difference);


    return 0;//STATE_OK;
}

any help you can offer would be appreciated on this.

Following the answers i got, PACE’s answer was spot on for what i wanted to-do and now i have a simpler bit of code that works perfectly for what i am trying to-do and is easier to under stand. below is the code in its modified form (it compiles perfectly on Linux btw).

#include <stdio.h>
#include <time.h>

int main ()
{
  time_t filetime;
  time_t presenttime;

  struct tm * timeinfo;

  time ( &filetime );
  time ( &presenttime);

  timeinfo = localtime ( &filetime );
  timeinfo->tm_hour = 14; //this should be entered from file
  timeinfo->tm_min = 15; //this should be entered from file
  filetime = mktime ( timeinfo );

  printf("my time %s\n",ctime(&filetime));
  printf("pc time %s\n",ctime(&presenttime));


    double difference = difftime(filetime, presenttime );

    printf("%lf\n",(double)filetime);
    printf("%lf\n",(double)presenttime);

    if (difference > 300.0) {
        // second is later than first
        printf("later\n");
    }
    else if (difference < 0.0) {
        // second is earlier than first
        printf("earlier\n");
    }
    printf("%lf\n", difference);


  return 0;

cheers for the help guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:19:06+00:00Added an answer on May 13, 2026 at 2:19 pm

    There is an example of making a time here. You can then compare the times with the difftime method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some expect advice on how to handle the following:- I have a
I need some help from the shell-script gurus out there. I have a .txt
I need some advice as to how I easily can separate test runs for
I need some info on how to use margins and how exactly padding works.
I need some software to explore and modify some SQLite databases. Does anything similar
We need some input on what is a good design pattern on using AJAX
I need some sort of interactive chart control for my .NET-based web app. I
I need some basic CMS functionality with rich text editing. On stack overflow there
I need some information about localization. I am using .net 2.0 with C# 2.0
I need some pointers on how to detect unknown hardware using .NET and C++/C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.