#pragma once /* * Copyright (c) 2016 MariaDB Corporation Ab * * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file and at www.mariadb.com/bsl11. * * Change Date: 2020-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2 or later of the General * Public License. */ #include #include #include #include namespace maxscale { class MonitorInstance : public MXS_MONITOR_INSTANCE { public: MonitorInstance(const MonitorInstance&) = delete; MonitorInstance& operator = (const MonitorInstance&) = delete; virtual ~MonitorInstance(); bool start(const MXS_CONFIG_PARAMETER* param); void stop(); protected: MonitorInstance(MXS_MONITOR* pMonitor); const std::string& script() const { return m_script; } uint64_t events() const { return m_events; } virtual bool has_sufficient_permissions() const; virtual void configure(const MXS_CONFIG_PARAMETER* pParams); virtual void tick() = 0; MXS_MONITOR* m_monitor; /**< The generic monitor structure. */ MXS_MONITORED_SERVER* m_master; /**< Master server */ private: int32_t m_status; /**< The current status of the monitor. */ THREAD m_thread; /**< The thread handle of the monitoring thread. */ int32_t m_shutdown; /**< Non-zero if the monitor should shut down. */ bool m_checked; /**< Whether server access has been checked. */ std::string m_script; /**< Launchable script. */ uint64_t m_events; /**< Enabled monitor events. */ Semaphore m_semaphore; /**< Semaphore for synchronizing with monitor thread. */ void main(); static void main(void* pArg); }; /** * The purpose of the template MonitorApi is to provide an implementation * of the monitor C-API. The template is instantiated with a class that * provides the actual behaviour of a monitor. */ template class MonitorApi { public: MonitorApi() = delete; MonitorApi(const MonitorApi&) = delete; MonitorApi& operator = (const MonitorApi&) = delete; static MXS_MONITOR_INSTANCE* createInstance(MXS_MONITOR* pMonitor) { MonitorInstance* pInstance = NULL; MXS_EXCEPTION_GUARD(pInstance = MonitorInstance::create(pMonitor)); return pInstance; } static void destroyInstance(MXS_MONITOR_INSTANCE* pInstance) { MXS_EXCEPTION_GUARD(static_cast(pInstance)->destroy()); } static bool startMonitor(MXS_MONITOR_INSTANCE* pInstance, const MXS_CONFIG_PARAMETER* pParams) { bool started = false; MXS_EXCEPTION_GUARD(started = static_cast(pInstance)->start(pParams)); return started; } static void stopMonitor(MXS_MONITOR_INSTANCE* pInstance) { MXS_EXCEPTION_GUARD(static_cast(pInstance)->stop()); } static void diagnostics(const MXS_MONITOR_INSTANCE* pInstance, DCB* pDcb) { MXS_EXCEPTION_GUARD(static_cast(pInstance)->diagnostics(pDcb)); } static json_t* diagnostics_json(const MXS_MONITOR_INSTANCE* pInstance) { json_t* pJson = NULL; MXS_EXCEPTION_GUARD(pJson = static_cast(pInstance)->diagnostics_json()); return pJson; } static MXS_MONITOR_API s_api; }; template MXS_MONITOR_API MonitorApi::s_api = { &MonitorApi::createInstance, &MonitorApi::destroyInstance, &MonitorApi::startMonitor, &MonitorApi::stopMonitor, &MonitorApi::diagnostics, &MonitorApi::diagnostics_json, }; }