00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00025 #include <dlfcn.h>
00026 #include <errno.h>
00027 #include <gio/gio.h>
00028 #include <glib.h>
00029 #include <glib/gstdio.h>
00030
00031 #include "application-data.h"
00032 #include "constants.h"
00033 #include "machine.h"
00034 #include "plugins.h"
00035
00036
00037
00054 static gint machine_parse_config_file (GFile *machine_file, machine_t *machine)
00055 {
00056 return 0;
00057 gchar *name = NULL;
00058 gchar *path = NULL;
00059 GError *err = NULL;
00060 GKeyFile *keyfile = g_key_file_new ();
00061
00062 path = g_file_get_path (machine_file);
00063 name = g_strdup_printf ("%s/%s", path, MACHINE_CONF_FILE);
00064 if (g_key_file_load_from_file (keyfile, name, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &err)) {
00065 g_free (path);
00066 g_free (name);
00067 g_debug ("Configuration for machine %s successfully loaded.\n", machine->display_name);
00068 g_key_file_free (keyfile);
00069 return 0;
00070 }
00071 else {
00072 g_warning ("%s", err->message);
00073 g_clear_error (&err);
00074 g_free (path);
00075 g_free (name);
00076 return -1;
00077 }
00078 }
00079
00080
00081
00096 static gint machine_identify_monitoring_tools (GFile *machine_file, machine_t *machine, GList *plugins)
00097 {
00098 gchar *name = NULL;
00099 gchar *path = NULL;
00100 GError *err = NULL;
00101 GFile *reports_dir = NULL;
00102 GFileEnumerator *reports_enum = NULL;
00103 GFileInfo *current = NULL;
00104
00105 path = g_file_get_path (machine_file);
00106 name = g_strdup_printf ("%s/%s", path, REPORTS_DIR);
00107 reports_dir = g_file_new_for_path (name);
00108 g_free (path);
00109 g_free (name);
00110
00111 reports_enum = g_file_enumerate_children (reports_dir, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, &err);
00112 if (err) {
00113 g_warning ("machine_identify_monitoring_tools::enumerate_children: %s", err->message);
00114 g_clear_error (&err);
00115 g_object_unref (reports_dir);
00116 return -1;
00117 }
00118
00119 while ((current = g_file_enumerator_next_file (reports_enum, NULL, &err)) != NULL) {
00120 if (err) {
00121 g_warning ("machine_identify_monitoring_tools::next_file: %s", err->message);
00122 g_clear_error (&err);
00123 } else {
00124 if (plugin_list_find_custom_list (plugins, g_file_info_get_name (current)))
00125 machine->monitoring_tools = g_list_prepend (machine->monitoring_tools,
00126 g_strdup (g_file_info_get_name (current)));
00127 g_object_unref (current);
00128 }
00129 }
00130
00131 g_object_unref (reports_enum);
00132 g_object_unref (reports_dir);
00133 return 0;
00134 }
00135
00136
00137
00152 static gint machine_get_info (GFile *top_dir, GFileInfo *info, machine_t *machine, GList *plugins)
00153 {
00154 gchar *name = NULL;
00155 gchar *path = NULL;
00156 GError *err = NULL;
00157 GFile *machine_file = NULL;
00158 GFileEnumerator *machine_enum = NULL;
00159 GFileInfo *current = NULL;
00160
00161 path = g_file_get_path (top_dir);
00162 name = g_strdup_printf ("%s/%s", path, g_file_info_get_name (info));
00163 machine_file = g_file_new_for_path (name);
00164 g_free (path);
00165 g_free (name);
00166
00167 machine_enum = g_file_enumerate_children (machine_file, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, &err);
00168 if (err) {
00169 g_warning ("machine_get_info::enumerate_children: %s", err->message);
00170 g_clear_error (&err);
00171 g_object_unref (machine_file);
00172 return -1;
00173 }
00174
00175 machine->_virtual = FALSE;
00176 machine->display_name = g_strdup (g_file_info_get_display_name (info));
00177 machine->folder_name = g_strdup (g_file_info_get_name (info));
00178 machine->monitoring_tools = NULL;
00179
00180 while ((current = g_file_enumerator_next_file (machine_enum, NULL, &err)) != NULL) {
00181 if (err) {
00182 g_warning ("machine_get_info::next_file: %s", err->message);
00183 g_clear_error (&err);
00184 err = NULL;
00185 } else {
00186 if (g_strcmp0 (g_file_info_get_name (current), MACHINE_CONF_FILE) == 0) {
00187 if (machine_parse_config_file (machine_file, machine)) {
00188 g_object_unref (current);
00189 g_object_unref (machine_file);
00190 return -1;
00191 }
00192 }
00193
00194 if (g_strcmp0 (g_file_info_get_name (current), REPORTS_DIR) == 0) {
00195 if (machine_identify_monitoring_tools (machine_file, machine, plugins)) {
00196 g_object_unref (current);
00197 g_object_unref (machine_file);
00198 return -1;
00199 }
00200 }
00201 g_object_unref (current);
00202 }
00203 }
00204
00205 g_object_unref (machine_enum);
00206 g_object_unref (machine_file);
00207 return 0;
00208 }
00209
00210
00211
00221 static void g_free_func (gpointer data, gpointer user_data)
00222 {
00223 g_free (data);
00224 }
00225
00226
00227
00236 static void machine_free (machine_t *machine)
00237 {
00238 g_return_if_fail (machine != NULL);
00239
00240 g_list_foreach (machine->monitoring_tools, g_free_func, NULL);
00241 g_list_free (machine->monitoring_tools);
00242
00243 g_free (machine->display_name);
00244 g_free (machine->folder_name);
00245 g_free (machine);
00246 }
00247
00248
00249
00259 static void machine_free_func (gpointer data, gpointer user_data)
00260 {
00261 machine_free ((machine_t *) data);
00262 }
00263
00264
00265
00266 GList *machine_list_new (const gchar *top_path, GList *plugins)
00267 {
00268 GError *err = NULL;
00269 GFile *top_dir = NULL;
00270 GFileEnumerator *dir_enum = NULL;
00271 GFileInfo *current = NULL;
00272 GList *list = NULL;
00273 machine_t *allm = NULL;
00274
00275 top_dir = g_file_new_for_path (top_path);
00276 dir_enum = g_file_enumerate_children (top_dir, "standard::name,standard::display-name", G_FILE_QUERY_INFO_NONE, NULL, &err);
00277 if (err) {
00278 g_warning ("machine_list_new::enumerate_children: %s", err->message);
00279 g_clear_error (&err);
00280 return NULL;
00281 }
00282
00283 allm = g_malloc (sizeof (machine_t));
00284 allm->_virtual = TRUE;
00285 allm->display_name = g_strdup (ALL_MACHINES_DISPLAY_NAME);
00286 allm->folder_name = g_strdup (ALL_MACHINES_FOLDER_NAME);
00287 allm->monitoring_tools = NULL;
00288
00289 while ((current = g_file_enumerator_next_file (dir_enum, NULL, &err)) != NULL) {
00290 if (err) {
00291 g_warning ("machine_list_new::next_file: %s", err->message);
00292 g_clear_error (&err);
00293 } else {
00294 if (g_strcmp0 (g_file_info_get_name (current), ALL_MACHINES_FOLDER_NAME) != 0) {
00295 machine_t *machine = g_malloc (sizeof (machine_t));
00296 list = g_list_prepend (list, machine);
00297 machine_get_info (top_dir, current, machine, plugins);
00298 g_object_unref (current);
00299 }
00300 }
00301 }
00302
00303 if (list) {
00304 list = g_list_append (list, allm);
00305 } else {
00306 machine_free (allm);
00307 }
00308
00309 g_object_unref (dir_enum);
00310 g_object_unref (top_dir);
00311 return list;
00312 }
00313
00314
00315
00316 void machine_list_free (GList *list)
00317 {
00318 g_return_if_fail (list != NULL);
00319
00320 g_list_foreach (list, machine_free_func, NULL);
00321 g_list_free (list);
00322 }
00323
00324
00325
00326 machine_t *machine_list_find_by_name (GList *list, const gchar *name)
00327 {
00328 GList *iter = list;
00329
00330 if (iter) {
00331 do {
00332 if (g_strcmp0 (machine_get_folder_name (iter->data), name) == 0)
00333 return iter->data;
00334 } while ((iter = g_list_next (iter)) != NULL);
00335 }
00336
00337 return NULL;
00338 }
00339
00340
00341
00351 static gint machine_cmp_func (gconstpointer m1, gconstpointer m2)
00352 {
00353 return machine_cmp ((machine_t *) m1, (machine_t *) m2);
00354 }
00355
00356
00357
00358 gint machine_list_remove (GList **list, machine_t *elem)
00359 {
00360 g_return_val_if_fail (*list != NULL, -1);
00361
00362 GList *elemptr = g_list_find_custom (*list, (gpointer) elem, machine_cmp_func);
00363 if (elemptr) {
00364 *list = g_list_remove (*list, elemptr->data);
00365 return 0;
00366 } else {
00367 return -1;
00368 }
00369 }
00370
00371
00372
00373 gint machine_cmp (machine_t *m1, machine_t *m2)
00374 {
00375 if (m1 == NULL)
00376 return (m2 == NULL) ? 0 : -1;
00377
00378 if (m2 == NULL)
00379 return 1;
00380
00381 return g_strcmp0 (m1->folder_name, m2->folder_name);
00382 }
00383
00384
00385
00386 const gchar *machine_get_display_name (machine_t *m)
00387 {
00388 return (m) ? m->display_name : NULL;
00389 }
00390
00391
00392
00393 const gchar *machine_get_folder_name (machine_t *m)
00394 {
00395 return (m) ? m->folder_name : NULL;
00396 }
00397
00398
00399
00400 gint machine_is_all_machines (machine_t *m)
00401 {
00402 g_return_val_if_fail (m != NULL, 0);
00403
00404 if ((m->_virtual == FALSE) || (g_strcmp0 (m->folder_name, ALL_MACHINES_FOLDER_NAME)))
00405 return 0;
00406 else
00407 return 1;
00408 }
00409
00410
00411
00412 GList *machine_get_list_for_plugin (const gchar *plugin_name, gboolean add_all_machines)
00413 {
00414 synema_instance_t *inst = synema_instance ();
00415 gchar *reports_dir = NULL;
00416 GList *final_list = NULL;
00417 GList *iter = inst->machines_list;
00418 struct stat buff;
00419
00420 if (iter) {
00421 do {
00422 if ((machine_is_all_machines (iter->data)) && (add_all_machines)) {
00423 final_list = g_list_prepend (final_list, iter->data);
00424 } else {
00425 reports_dir = g_strdup_printf ("%s/%s/"REPORTS_DIR"/%s",
00426 inst->machines_dir,
00427 machine_get_folder_name (iter->data),
00428 plugin_name);
00429
00430 if (g_stat (reports_dir, &buff) == 0) {
00431 if (S_ISDIR (buff.st_mode))
00432 final_list = g_list_prepend (final_list, iter->data);
00433 }
00434
00435 g_free (reports_dir);
00436 }
00437 } while ((iter = g_list_next (iter)) != NULL);
00438 }
00439
00440 return final_list;
00441 }