Esempio n. 1
0
/*  ... the main entry point for the ssh process
*/
int main (int argc, char *argv[])
{
    void            (*alarmHandler)(int);
    STIM            stim;
    int             i;
    int             seconds;
    time_t          ntime;
    struct tm       locTime;
    int             offset, retVal;
    long            msOffset;
    FILE            *pidfile;
    int             runAsDaemon = TRUE;

    if (argc > 1)
    {
        if (!strcmp(argv[1], "-f"))
        {
            runAsDaemon = FALSE;
        }
    }

    memset (&sshWork, 0, sizeof (sshWork));

    /*  ... initialize some system stuff first
    */
    retVal = sshSysInit (&sshWork);
    if (retVal == ERROR)
    {
        radMsgLogInit (PROC_NAME_SSH, FALSE, TRUE);
        radMsgLog (PRI_CATASTROPHIC, "ssh init failed!");
        radMsgLogExit ();
        exit (1);
    }
    else if (retVal == ERROR_ABORT)
    {
        exit (2);
    }


    /*  ... call the global radlib system init function
    */
    if (radSystemInit (WVIEW_SYSTEM_ID) == ERROR)
    {
        radMsgLogInit (PROC_NAME_SSH, TRUE, TRUE);
        radMsgLog (PRI_CATASTROPHIC, "radSystemInit failed!");
        radMsgLogExit ();
        exit (1);
    }


    /*  ... call the radlib process init function
    */
    if (radProcessInit (PROC_NAME_SSH,
                        sshWork.fifoFile,
                        PROC_NUM_TIMERS_SSH,
                        runAsDaemon,                // TRUE for daemon
                        msgHandler,
                        evtHandler,
                        NULL)
        == ERROR)
    {
        printf ("\nradProcessInit failed: %s\n\n", PROC_NAME_SSH);
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    sshWork.myPid = getpid ();
    pidfile = fopen (sshWork.pidFile, "w");
    if (pidfile == NULL)
    {
        radMsgLog (PRI_CATASTROPHIC, "lock file create failed!");
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }
    fprintf (pidfile, "%d", getpid ());
    fclose (pidfile);


    alarmHandler = radProcessSignalGetHandler (SIGALRM);
    radProcessSignalCatchAll (SSHDefaultSigHandler);
    radProcessSignalCatch (SIGALRM, alarmHandler);
    radProcessSignalRelease(SIGABRT);


    sshWork.timer = radTimerCreate (NULL, timerHandler, NULL);
    if (sshWork.timer == NULL)
    {
        radMsgLog (PRI_HIGH, "radTimerCreate failed - exiting");
        sshSysExit (&sshWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    //  ... initialize the ssh utilities
    retVal = sshUtilsInit (&sshWork.sshData);
    if (retVal != OK)
    {
        if (retVal == ERROR)
        {
            radMsgLog (PRI_HIGH, "sshUtilsInit failed - exiting");
        }
        radTimerDelete (sshWork.timer);
        sshSysExit (&sshWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }
    else
    {
        sshWork.sshId = &sshWork.sshData;
    }

    if (statusInit(sshWork.statusFile, sshStatusLabels) == ERROR)
    {
        radMsgLog (PRI_HIGH, "statusInit failed - exiting...");
        sshSysExit (&sshWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }
    statusUpdate(STATUS_BOOTING);
    statusUpdateStat(SSH_STATS_RULES_DEFINED, 
                     radListGetNumberOfNodes(&sshWork.sshData.rules));

    //  ... start THE timer
    ntime = time (NULL);
    localtime_r (&ntime, &locTime);
    seconds = locTime.tm_sec - 15;          // start at 15 secs past

    /*  ... start the ssh timer to go off 1 min past the next archive record
    */
    offset = locTime.tm_min % 5;
    if (offset)
        offset = 6 - offset;
    else
        offset = 1;

    if (seconds < -60)
    {
        offset += 1;
        offset += 60;
    }

    radMsgLog (PRI_HIGH,
               "SSH: starting updates in %d mins %d secs",
               ((seconds > 0) ? ((offset > 0) ? offset-1 : offset) : offset),
                       (seconds > 0) ? 60 - seconds : (-1) * seconds);

    msOffset = radTimeGetMSSinceEpoch () % 1000;
    msOffset -= 250;                        // land on 250 ms mark


    sshWork.msOffset = 0;
    radProcessTimerStart (sshWork.timer,
                          ((((offset * 60) - seconds) * 1000)) - msOffset);

    statusUpdate(STATUS_RUNNING);
    statusUpdateMessage("Normal operation");


    while (!sshWork.exiting)
    {
        /*  ... wait on timers, events, file descriptors, msgs, everything!
        */
        if (radProcessWait (0) == ERROR)
        {
            sshWork.exiting = TRUE;
        }
    }


    statusUpdateMessage("exiting normally");
    radMsgLog (PRI_STATUS, "exiting normally...");
    statusUpdate(STATUS_SHUTDOWN);

    if (sshWork.sshId != NULL)
    {
        sshUtilsExit (sshWork.sshId);
    }
    radTimerDelete (sshWork.timer);
    sshSysExit (&sshWork);
    radProcessExit ();
    radSystemExit (WVIEW_SYSTEM_ID);
    exit (0);
}
Esempio n. 2
0
HTML_MGR_ID htmlmgrInit
(
    char            *installPath,
    int             isMetricUnits,
    char            *imagePath,
    char            *htmlPath,
    int             arcInterval,
    int             isExtendedData,
    char            *name,
    char            *city,
    char            *state,
    int16_t         elevation,
    int16_t         latitude,
    int16_t         longitude,
    char            *mphaseIncrease,
    char            *mphaseDecrease,
    char            *mphaseFull,
    char            *radarURL,
    char            *forecastURL,
    char            *dateFormat,
    int             isDualUnits
)
{
    HTML_MGR_ID     newId;
    int             i, numNodes, numImages, numTemplates;
    char            confFilePath[256];

    newId = &mgrWork;
    memset (newId, 0, sizeof (mgrWork));

    radListReset (&newId->imgList);
    radListReset (&newId->templateList);
    
    newId->isMetricUnits    = isMetricUnits;
    newId->archiveInterval  = arcInterval;
    newId->isExtendedData   = isExtendedData;
    newId->imagePath        = imagePath;
    newId->htmlPath         = htmlPath;
    newId->stationElevation = elevation;
    newId->stationLatitude  = latitude;
    newId->stationLongitude = longitude;
    wvstrncpy (newId->stationName, name, sizeof(newId->stationName));
    wvstrncpy (newId->stationCity, city, sizeof(newId->stationCity));
    wvstrncpy (newId->stationState, state, sizeof(newId->stationState));
    wvstrncpy (newId->mphaseIncrease, mphaseIncrease, sizeof(newId->mphaseIncrease));
    wvstrncpy (newId->mphaseDecrease, mphaseDecrease, sizeof(newId->mphaseDecrease));
    wvstrncpy (newId->mphaseFull, mphaseFull, sizeof(newId->mphaseFull));
    wvstrncpy (newId->radarURL, radarURL, sizeof(newId->radarURL));
    wvstrncpy (newId->forecastURL, forecastURL, sizeof(newId->forecastURL));
    wvstrncpy (newId->dateFormat, dateFormat, sizeof(newId->dateFormat));
    newId->isDualUnits    = isDualUnits;
    
    //  ... initialize the newArchiveMask
    newId->newArchiveMask = NEW_ARCHIVE_ALL;

    
    //  ... first, the built-in generators
    sprintf (confFilePath, "%s/images.conf", installPath);
    if (readImageConfFile (newId, confFilePath, FALSE) != OK)
    {
        return NULL;
    }
    else
    {
        numNodes = radListGetNumberOfNodes (&newId->imgList);
        radMsgLog (PRI_STATUS, "htmlmgrInit: %d built-in image definitions added",
                   numNodes);
        numImages = numNodes;
    }

    //  ... next, the user-defined generators
    sprintf (confFilePath, "%s/images-user.conf", installPath);
    i = readImageConfFile (newId, confFilePath, TRUE);
    if (i == ERROR_ABORT)
    {
        radMsgLog (PRI_STATUS, "htmlmgrInit: no user image definitions found");
    }
    else if (i == ERROR)
    {
        return NULL;
    }
    else
    {
        numImages = radListGetNumberOfNodes (&newId->imgList);
        radMsgLog (PRI_STATUS, "htmlmgrInit: %d user image definitions added",
                   radListGetNumberOfNodes (&newId->imgList) - numNodes);
    }

    //  ... now initialize our html template list
    sprintf (confFilePath, "%s/html-templates.conf", installPath);
    if (readHtmlTemplateFile (newId, confFilePath) != OK)
    {
        return NULL;
    }
    else
    {
        numTemplates = radListGetNumberOfNodes (&newId->templateList);
        radMsgLog (PRI_STATUS, "htmlmgrInit: %d templates added", numTemplates);
    }

    //  ... now initialize our forecast rule text list
    sprintf (confFilePath, "%s/forecast.conf", installPath);
    if (readForecastRuleConfigFile (newId, confFilePath) != OK)
    {
        radMsgLog (PRI_STATUS, "htmlmgrInit: forecast html tags are disabled - %s not found...",
                   confFilePath);
    }

    //  ... initialize the sample label array
    htmlmgrSetSampleLabels(newId);

    //  ... initialize our dial palette to conserve CPU cycles
    if (htmlGenPngDialInit (newId->imagePath) == ERROR)
    {
        radMsgLog (PRI_HIGH, "htmlmgrInit: htmlGenPngDialInit failed!");
        return NULL;
    }

    statusUpdateStat(HTML_STATS_IMAGES_DEFINED, numImages);
    statusUpdateStat(HTML_STATS_TEMPLATES_DEFINED, numTemplates);

    return newId;
}
Esempio n. 3
0
int htmlmgrGenerate
(
    HTML_MGR_ID         id
)
{
    register HTML_IMG   *img;
    int                 retVal, imgs = 0, htmls = 0;
    char                temp[256];
    struct stat         fileData;

    GenerateTime = radTimeGetMSSinceEpoch ();

#if __DEBUG_BUFFERS
    radMsgLog (PRI_STATUS, "DBG BFRS: HTML BEGIN: %u of %u available",
               buffersGetAvailable (),
               buffersGetTotal ());
#endif

    //  ... compute the Barometric Pressure trend
    computeBPTrend (id);

#if DEBUG_GENERATION
    radMsgLog (PRI_MEDIUM, "GENERATE: images");
#endif

    //  ... generate the weather images
    for (img = (HTML_IMG *)radListGetFirst (&id->imgList);
         img != NULL;
         img = (HTML_IMG *)radListGetNext (&id->imgList, (NODE_PTR)img))
    {
        retVal = (*img->generator) (img);
        if (retVal == OK)
        {
            imgs ++;
        }
        else if (retVal != ERROR_ABORT)
        {
            sprintf (temp, "%s/%s", id->imagePath, img->fname);
            radMsgLog (PRI_HIGH, "%s generation failed - must be local to the wview server!",
                       temp);
            radMsgLog (PRI_HIGH, "Otherwise you may be including data in "
                                 "images.conf for which you do not have sensors?!?");
        }
    }

    //  ... clear the archiveAvailable flag (must be after generator loop)
    id->newArchiveMask = 0;

#if DEBUG_GENERATION
    radMsgLog (PRI_MEDIUM, "GENERATE: pre-generate script");
#endif

    // If the wview pre-generation script exists, run it now
    sprintf (temp, "%s/%s", WVIEW_CONFIG_DIR, HTML_PRE_GEN_SCRIPT);
    if (stat (temp, &fileData) == 0)
    {
        // File exists, run it
        radStartProcess (newProcessEntryPoint, temp);
    }

#if DEBUG_GENERATION
    radMsgLog (PRI_MEDIUM, "GENERATE: templates");
#endif

    //  ... now generate the HTML
    if ((htmls = htmlgenOutputFiles(id, GenerateTime)) == ERROR)
    {
        return ERROR;
    }

    wvutilsLogEvent(PRI_STATUS, "Generated: %u ms: %d images, %d template files",
                    (uint32_t)(radTimeGetMSSinceEpoch() - GenerateTime), imgs, htmls);

    id->imagesGenerated += imgs;
    id->templatesGenerated += htmls;
    statusUpdateStat(HTML_STATS_IMAGES_GENERATED, id->imagesGenerated);
    statusUpdateStat(HTML_STATS_TEMPLATES_GENERATED, id->templatesGenerated);

#if __DEBUG_BUFFERS
    radMsgLog (PRI_STATUS, "DBG BFRS: HTML END: %u of %u available",
               buffersGetAvailable (),
               buffersGetTotal ());
#endif

#if DEBUG_GENERATION
    radMsgLog (PRI_MEDIUM, "GENERATE: post-generate script");
#endif

    // Finally, if the wview post-generation script exists, run it now
    sprintf (temp, "%s/%s", WVIEW_CONFIG_DIR, HTML_POST_GEN_SCRIPT);
    if (stat (temp, &fileData) == 0)
    {
        // File exists, run it
        radStartProcess (newProcessEntryPoint, temp);
    }

#if DEBUG_GENERATION
    radMsgLog (PRI_MEDIUM, "GENERATE: DONE");
#endif

    return OK;
}
Esempio n. 4
0
File: alarms.c Progetto: breu/wview
/*  ... the main entry point for the alarm process
*/
int main (int argc, char *argv[])
{
    void            (*alarmHandler)(int);
    int             retVal;
    FILE            *pidfile;
    WVIEW_ALARM     *alarm;
    int             runAsDaemon = TRUE;

    if (argc > 1)
    {
        if (!strcmp(argv[1], "-f"))
        {
            runAsDaemon = FALSE;
        }
    }

    memset (&alarmsWork, 0, sizeof (alarmsWork));
    radListReset (&alarmsWork.alarmList);
    radListReset (&alarmsWork.clientList);

    /*  ... initialize some system stuff first
    */
    retVal = alarmsSysInit (&alarmsWork);
    if (retVal == ERROR)
    {
        radMsgLogInit (PROC_NAME_ALARMS, FALSE, TRUE);
        radMsgLog (PRI_CATASTROPHIC, "wvalarmd sysinit failed!");
        radMsgLogExit ();
        exit (1);
    }
    else if (retVal == ERROR_ABORT)
    {
        exit (2);
    }


    /*  ... call the global radlib system init function
    */
    if (radSystemInit (WVIEW_SYSTEM_ID) == ERROR)
    {
        radMsgLogInit (PROC_NAME_ALARMS, TRUE, TRUE);
        radMsgLog (PRI_CATASTROPHIC, "radSystemInit failed!");
        radMsgLogExit ();
        exit (1);
    }


    /*  ... call the radlib process init function
    */
    if (radProcessInit (PROC_NAME_ALARMS,
                        alarmsWork.fifoFile,
                        PROC_NUM_TIMERS_ALARMS,
                        runAsDaemon,                // TRUE for daemon
                        msgHandler,
                        evtHandler,
                        NULL)
        == ERROR)
    {
        printf ("\nradProcessInit failed: %s\n\n", PROC_NAME_ALARMS);
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    alarmsWork.myPid = getpid ();
    pidfile = fopen (alarmsWork.pidFile, "w");
    if (pidfile == NULL)
    {
        radMsgLog (PRI_CATASTROPHIC, "lock file create failed!");
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }
    fprintf (pidfile, "%d", getpid ());
    fclose (pidfile);

    alarmHandler = radProcessSignalGetHandler (SIGALRM);
    radProcessSignalCatchAll (defaultSigHandler);
    radProcessSignalCatch (SIGALRM, alarmHandler);
    radProcessSignalRelease(SIGABRT);


    // grab all of our alarm definitions from the config database
    retVal = readAlarmsConfig ();
    if (retVal == ERROR_ABORT)
    {
        radMsgLog (PRI_HIGH, "ALARM daemon not enabled - exiting...");
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (0);
    }
    else if (retVal < 0)
    {
        radMsgLog (PRI_HIGH, "readAlarmsConfig failed - "
                   "is there a problem with the wview config database?");
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }
    else
    {
        radMsgLog (PRI_STATUS, 
                   "alarms: added %d alarm definitions",
                   retVal);
    }

    if (statusInit(alarmsWork.statusFile, alarmsStatusLabels) == ERROR)
    {
        radMsgLog (PRI_HIGH, "ALARM status init failed - exiting...");
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    statusUpdate(STATUS_BOOTING);
    statusUpdateStat(ALARM_STATS_ALARMS, retVal);


    // wait a bit here before continuing
    radUtilsSleep (500);


    //  register with the radlib message router
    if (radMsgRouterInit (WVIEW_RUN_DIR) == ERROR)
    {
        statusUpdateMessage("radMsgRouterInit failed!");
        radMsgLog (PRI_HIGH, "radMsgRouterInit failed!");
        statusUpdate(STATUS_ERROR);
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    // enable message reception from the radlib router for SHUTDOWN msgs
    radMsgRouterMessageRegister (WVIEW_MSG_TYPE_SHUTDOWN);

    // wait for the wview daemon to be ready
    if (waitForWviewDaemon () == ERROR)
    {
        radMsgLog (PRI_HIGH, "waitForWviewDaemon failed");
        statusUpdate(STATUS_ERROR);
        radMsgRouterExit ();
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    // finally, initialize our data feed socket
    alarmsWork.dataFeedServer = radSocketServerCreate(WV_DATAFEED_PORT);
    if (alarmsWork.dataFeedServer == NULL)
    {
        statusUpdateMessage("radSocketServerCreate failed");
        radMsgLog (PRI_HIGH, "radSocketServerCreate failed...");
        statusUpdate(STATUS_ERROR);
        radMsgRouterExit ();
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    // add it to our descriptors of interest
    if (radProcessIORegisterDescriptor(radSocketGetDescriptor(alarmsWork.dataFeedServer),
                                       dataFeedAccept,
                                       NULL)
        == ERROR)
    {
        statusUpdateMessage("radProcessIORegisterDescriptor server failed");
        radMsgLog (PRI_HIGH, "radProcessIORegisterDescriptor failed...");
        statusUpdate(STATUS_ERROR);
        radSocketDestroy (alarmsWork.dataFeedServer);
        radMsgRouterExit ();
        alarmsSysExit (&alarmsWork);
        radProcessExit ();
        radSystemExit (WVIEW_SYSTEM_ID);
        exit (1);
    }

    // enable message reception from the radlib router for loop data
    radMsgRouterMessageRegister (WVIEW_MSG_TYPE_LOOP_DATA_SVC);

    // enable message reception from the radlib router for archive data
    radMsgRouterMessageRegister (WVIEW_MSG_TYPE_ARCHIVE_DATA);

    // enable message reception from the radlib router for POLL msgs
    radMsgRouterMessageRegister (WVIEW_MSG_TYPE_POLL);


    // enter normal processing
    alarmsWork.inMainLoop = TRUE;
    statusUpdate(STATUS_RUNNING);
    statusUpdateMessage("Normal operation");
    radMsgLog (PRI_STATUS, "running...");


    // Do we need to trigger a test alarm?
    if (alarmsWork.doTest)
    {
        if (1 <= alarmsWork.doTestNumber && alarmsWork.doTestNumber <= ALARMS_MAX)
        {
            // Generate the bad boy:
            retVal = 1;
            for (alarm = (WVIEW_ALARM *) radListGetFirst (&alarmsWork.alarmList);
                 retVal <= ALARMS_MAX && alarm != NULL;
                 alarm = (WVIEW_ALARM *) radListGetNext (&alarmsWork.alarmList, 
                                                         (NODE_PTR)alarm))
            {
                if (retVal == alarmsWork.doTestNumber)
                {
                    // This is the one to test:
                    alarm->triggerValue = -1;

                    // run user script here
                    if (executeScript(alarm) != 0)
                    {
                        radMsgLog (PRI_MEDIUM, 
                                   "Test Alarm %d: script %s failed",
                                   retVal, alarm->scriptToRun);
                    }
                    else
                    {
                        radMsgLog (PRI_MEDIUM, 
                                   "Test Alarm %d: script %s executed",
                                   retVal, alarm->scriptToRun);
                    }
                    retVal = ALARMS_MAX;
                }

                retVal ++;
            }
        }
        else
        {
            radMsgLog (PRI_MEDIUM, "Test Alarm: bad alarm index %d given!",
                       alarmsWork.doTestNumber);
        }
    }


    while (!alarmsWork.exiting)
    {
        // wait on something interesting
        if (radProcessWait (0) == ERROR)
        {
            alarmsWork.exiting = TRUE;
        }
    }


    statusUpdateMessage("exiting normally");
    radMsgLog (PRI_STATUS, "exiting normally...");
    statusUpdate(STATUS_SHUTDOWN);

    radSocketDestroy (alarmsWork.dataFeedServer);
    radMsgRouterExit ();
    alarmsSysExit (&alarmsWork);
    radProcessExit ();
    radSystemExit (WVIEW_SYSTEM_ID);
    exit (0);
}