source: ogServer-Git/src/main.c

Last change on this file was f3422f6, checked in by OpenGnSys Support Team <soporte-og@…>, 2 years ago

#915 add seconds since ogserver has been launched

Extend GET /stats to show the number of seconds since the ogserver started.

{

"time": {

"now": 1647262765, /* Seconds since 1970 */
"boot": 2151909 /* Seconds since boot */
"start" : 1647262854 /* Seconds since 1970 */

},
[...]

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
10#include "ogAdmServer.h"
11#include "dbi.h"
12#include "utils.h"
13#include "list.h"
14#include "rest.h"
15#include "client.h"
16#include "json.h"
17#include "schedule.h"
18#include "core.h"
19#include "cfg.h"
20#include <syslog.h>
21#include <getopt.h>
22
23static struct option og_server_opts[] = {
24        { "config-file", 1, 0, 'f' },
25        { NULL },
26};
27
28#define OG_SERVER_CFG_JSON      "/opt/opengnsys/cfg/ogserver.json"
29#define OG_SERVER_REPO_PATH     "/opt/opengnsys/images"
30
31struct og_server_cfg ogconfig = {
32        .repo   = {
33                .dir    = OG_SERVER_REPO_PATH,
34        },
35};
36
37time_t start_time;
38
39int main(int argc, char *argv[])
40{
41        char config_file[PATH_MAX + 1] = OG_SERVER_CFG_JSON;
42        struct ev_io ev_io_server_rest, ev_io_agent_rest;
43        int val;
44
45        start_time = time(NULL);
46
47        og_loop = ev_default_loop(0);
48
49        if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
50                exit(EXIT_FAILURE);
51
52        openlog("ogserver", LOG_PID, LOG_DAEMON);
53
54        while (1) {
55                val = getopt_long(argc, argv, "f:l:d:", og_server_opts, NULL);
56                if (val < 0)
57                        break;
58
59                switch (val) {
60                case 'f':
61                        snprintf(config_file, sizeof(config_file), "%s", optarg);
62                        break;
63                case 'l':
64                case 'd':
65                        /* ignore, legacy options */
66                        break;
67                case '?':
68                        return EXIT_FAILURE;
69                default:
70                        break;
71                }
72        }
73
74        if (parse_json_config(config_file, &ogconfig) < 0)
75                return EXIT_FAILURE;
76
77        socket_rest = og_socket_server_init(ogconfig.rest.port);
78        if (socket_rest < 0) {
79                syslog(LOG_ERR, "Cannot open REST API server socket\n");
80                exit(EXIT_FAILURE);
81        }
82
83        ev_io_init(&ev_io_server_rest, og_server_accept_cb, socket_rest, EV_READ);
84        ev_io_start(og_loop, &ev_io_server_rest);
85
86        socket_agent_rest = og_socket_server_init("8889");
87        if (socket_agent_rest < 0) {
88                syslog(LOG_ERR, "Cannot open ogClient server socket\n");
89                exit(EXIT_FAILURE);
90        }
91
92        ev_io_init(&ev_io_agent_rest, og_server_accept_cb, socket_agent_rest, EV_READ);
93        ev_io_start(og_loop, &ev_io_agent_rest);
94
95        if (og_dbi_schema_update() < 0)
96                exit(EXIT_FAILURE);
97
98        if (og_dbi_schedule_get() < 0) {
99                syslog(LOG_ERR, "Cannot connect to database\n");
100                exit(EXIT_FAILURE);
101        }
102
103        og_schedule_next(og_loop);
104
105        syslog(LOG_INFO, "Waiting for connections\n");
106
107        while (1)
108                ev_loop(og_loop, 0);
109
110        exit(EXIT_SUCCESS);
111}
Note: See TracBrowser for help on using the repository browser.