]> git.stg.codes - ssmd.git/commitdiff
Control dumping snmp scripts.
authorMaxim Mamontov <faust.madf@gmail.com>
Wed, 17 Sep 2014 17:46:43 +0000 (20:46 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Wed, 17 Sep 2014 17:46:43 +0000 (20:46 +0300)
Makefile
include/settings.h
include/settings.inl.h
src/settings.cpp
src/settingsfileparser.cpp
src/switch.cpp

index 45aef9a28ae52d773390a5a21aead017d75d2dc3..895972072e4140a638530c1b28e2377c448f8509 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ LIBS = 3rdparty/snmp++/libsnmp++.a \
        -lcrypto \
        -lpthread
 PROG = ssmd # SNMP Switch Management Daemon
        -lcrypto \
        -lpthread
 PROG = ssmd # SNMP Switch Management Daemon
-VERSION = 1.1.7
+VERSION = 1.2.0
 
 SOURCES = src/main.cpp \
          src/pidfile.cpp \
 
 SOURCES = src/main.cpp \
          src/pidfile.cpp \
index c88f94a6c7694f5a29ee19265390926641776d12..041453a9a3bb1cd86b2d7a21d839fc5185a40667 100644 (file)
@@ -40,6 +40,9 @@ class Settings {
 
         const std::string & dataURL() const throw() { return _dataURL; }
 
 
         const std::string & dataURL() const throw() { return _dataURL; }
 
+        const std::string & scriptBase() const throw() { return _scriptBase; }
+        bool dumpScripts() const throw() { return _dumpScripts; }
+
         // Setters
         void setIsHelp(bool value) throw() { _isHelp = value; }
         void setIsVersion(bool value) throw() { _isVersion = value; }
         // Setters
         void setIsHelp(bool value) throw() { _isHelp = value; }
         void setIsVersion(bool value) throw() { _isVersion = value; }
@@ -80,6 +83,9 @@ class Settings {
 
         std::string _dataURL;
 
 
         std::string _dataURL;
 
+        std::string _scriptBase;
+        bool _dumpScripts;
+
         friend class SettingsParser;
 };
 
         friend class SettingsParser;
 };
 
index dbc275cb852fcc7ee8854a07b3872b1819a5f775..e922c82dd7be4ae08229b3567d21f9723d795a2b 100644 (file)
@@ -20,7 +20,7 @@ Settings::Settings()
       _upProfileId(1),
       _downProfileId(2),
       _maxACLPerPDU(50),
       _upProfileId(1),
       _downProfileId(2),
       _maxACLPerPDU(50),
-      _dataURL()
+      _dumpScripts(false)
 {
 }
 
 {
 }
 
@@ -38,7 +38,9 @@ Settings::Settings(const Settings & rvalue)
       _upProfileId(rvalue._upProfileId),
       _downProfileId(rvalue._downProfileId),
       _maxACLPerPDU(rvalue._maxACLPerPDU),
       _upProfileId(rvalue._upProfileId),
       _downProfileId(rvalue._downProfileId),
       _maxACLPerPDU(rvalue._maxACLPerPDU),
-      _dataURL(rvalue._dataURL)
+      _dataURL(rvalue._dataURL),
+      _scriptBase(rvalue._scriptBase),
+      _dumpScripts(rvalue._dumpScripts)
 {
 }
 
 {
 }
 
@@ -63,6 +65,8 @@ const Settings & Settings::operator=(const Settings & rvalue)
     _downProfileId = rvalue._downProfileId;
     _maxACLPerPDU = rvalue._maxACLPerPDU;
     _dataURL = rvalue._dataURL;
     _downProfileId = rvalue._downProfileId;
     _maxACLPerPDU = rvalue._maxACLPerPDU;
     _dataURL = rvalue._dataURL;
+    _scriptBase = rvalue._scriptBase;
+    _dumpScripts = rvalue._dumpScripts;
     return *this;
 }
 
     return *this;
 }
 
index 6e3a7913c5e4b2463b1e169206a5791b49f8a30f..897793ce880f879f30f370fbf86d635870dd4d4d 100644 (file)
@@ -21,6 +21,8 @@ SettingsParser::SettingsParser()
         ("down-profile-id", po::value<unsigned>(), "switch's download profile id")
         ("max-acl-per-pdu", po::value<size_t>(), "maximum ACL's per PDU")
         ("data-url", po::value<std::string>(), "data access URL")
         ("down-profile-id", po::value<unsigned>(), "switch's download profile id")
         ("max-acl-per-pdu", po::value<size_t>(), "maximum ACL's per PDU")
         ("data-url", po::value<std::string>(), "data access URL")
+        ("script-base", po::value<std::string>(), "base dir for scripts")
+        ("dump-scripts", "dump SNMP command into shell scripts")
         ("version,v", "show ssmd version and exit")
     ;
 }
         ("version,v", "show ssmd version and exit")
     ;
 }
@@ -29,14 +31,13 @@ void SettingsParser::init(int argc, char * argv[])
 {
     po::variables_map vm;
     po::store(po::parse_command_line(argc, argv, _desc), vm);
 {
     po::variables_map vm;
     po::store(po::parse_command_line(argc, argv, _desc), vm);
-    po::notify(vm);    
+    po::notify(vm);
 
     _settings._isHelp = vm.count("help");
     _settings._isVersion = vm.count("version");
 
 
     _settings._isHelp = vm.count("help");
     _settings._isVersion = vm.count("version");
 
-    if (vm.count("config")) {
+    if (vm.count("config"))
         _settings._configFile = vm["config"].as<std::string>();
         _settings._configFile = vm["config"].as<std::string>();
-    }
 
     if (!_settings._isHelp &&
         !_settings._isVersion) {
 
     if (!_settings._isHelp &&
         !_settings._isVersion) {
@@ -48,44 +49,31 @@ void SettingsParser::init(int argc, char * argv[])
         }
     }
 
         }
     }
 
-    if (vm.count("debug")) {
+    if (vm.count("debug"))
         _settings._isDebug = true;
         _settings._isDebug = true;
-    }
-    if (vm.count("daemon")) {
+    if (vm.count("daemon"))
         _settings._isDaemon = true;
         _settings._isDaemon = true;
-    }
 
 
-    if (vm.count("log-file")) {
+    if (vm.count("log-file"))
         _settings._logFile = vm["log-file"].as<std::string>();
         _settings._logFile = vm["log-file"].as<std::string>();
-    }
-
-    if (vm.count("pid-file")) {
+    if (vm.count("pid-file"))
         _settings._PIDFile = vm["pid-file"].as<std::string>();
         _settings._PIDFile = vm["pid-file"].as<std::string>();
-    }
-
-    if (vm.count("switch-sync-interval")) {
+    if (vm.count("switch-sync-interval"))
         _settings._switchSyncInterval = vm["switch-sync-interval"].as<time_t>();
         _settings._switchSyncInterval = vm["switch-sync-interval"].as<time_t>();
-    }
-
-    if (vm.count("info-sync-interval")) {
+    if (vm.count("info-sync-interval"))
         _settings._infoSyncInterval = vm["info-sync-interval"].as<time_t>();
         _settings._infoSyncInterval = vm["info-sync-interval"].as<time_t>();
-    }
-
-    if (vm.count("up-profile-id")) {
+    if (vm.count("up-profile-id"))
         _settings._upProfileId = vm["up-profile-id"].as<unsigned>();
         _settings._upProfileId = vm["up-profile-id"].as<unsigned>();
-    }
-
-    if (vm.count("down-profile-id")) {
+    if (vm.count("down-profile-id"))
         _settings._downProfileId = vm["down-profile-id"].as<unsigned>();
         _settings._downProfileId = vm["down-profile-id"].as<unsigned>();
-    }
-
-    if (vm.count("max-acl-per-pdu")) {
+    if (vm.count("max-acl-per-pdu"))
         _settings._downProfileId = vm["max-acl-per-pdu"].as<unsigned>();
         _settings._downProfileId = vm["max-acl-per-pdu"].as<unsigned>();
-    }
-
-    if (vm.count("data-url")) {
+    if (vm.count("data-url"))
         _settings._dataURL = vm["data-url"].as<std::string>();
         _settings._dataURL = vm["data-url"].as<std::string>();
-    }
+    if (vm.count("script-base"))
+        _settings._scriptBase = vm["script-base"].as<std::string>();
+    if (vm.count("dump-scripts"))
+        _settings._dumpScripts = true;
 }
 
 void SettingsParser::reloadConfig()
 }
 
 void SettingsParser::reloadConfig()
index bf2fda2ca27a2bb92c0c023da7c36df21947162d..a21130766a8f997e23d50f4f9120b39e0be199df 100644 (file)
@@ -16,7 +16,7 @@ typedef std::map<std::string, std::string> PairsType;
 typedef std::map<std::string, PairsType> SectionsType;
 
 template <typename Iterator>
 typedef std::map<std::string, PairsType> SectionsType;
 
 template <typename Iterator>
-struct IniGrammar 
+struct IniGrammar
   : qi::grammar<Iterator, SectionsType()>
 {
     IniGrammar()
   : qi::grammar<Iterator, SectionsType()>
 {
     IniGrammar()
@@ -77,9 +77,8 @@ using SSMD::SettingsParser;
 void SettingsParser::parseFile(const std::string & fileName)
 {
     std::ifstream in(fileName.c_str());
 void SettingsParser::parseFile(const std::string & fileName)
 {
     std::ifstream in(fileName.c_str());
-    if (!in) {
+    if (!in)
         throw std::runtime_error("Can't open file");
         throw std::runtime_error("Can't open file");
-    }
 
     std::string text;
     while(!in.eof()) {
 
     std::string text;
     while(!in.eof()) {
@@ -101,28 +100,23 @@ void SettingsParser::parseFile(const std::string & fileName)
     std::string res;
     _settings._isDaemon = fieldExists(data, "general", "daemon");
     _settings._isDebug = fieldExists(data, "general", "debug");
     std::string res;
     _settings._isDaemon = fieldExists(data, "general", "daemon");
     _settings._isDebug = fieldExists(data, "general", "debug");
-    if (fieldValue(data, "general", "log_file", res)){
+    if (fieldValue(data, "general", "log_file", res))
         _settings._logFile = res;
         _settings._logFile = res;
-    }
-    if (fieldValue(data, "general", "pid_file", res)){
+    if (fieldValue(data, "general", "pid_file", res))
         _settings._PIDFile = res;
         _settings._PIDFile = res;
-    }
-    if (fieldValue(data, "sync", "switch_interval", res)){
+    if (fieldValue(data, "sync", "switch_interval", res))
         _settings._switchSyncInterval = boost::lexical_cast<time_t>(res);
         _settings._switchSyncInterval = boost::lexical_cast<time_t>(res);
-    }
-    if (fieldValue(data, "sync", "info_interval", res)){
+    if (fieldValue(data, "sync", "info_interval", res))
         _settings._infoSyncInterval = boost::lexical_cast<time_t>(res);
         _settings._infoSyncInterval = boost::lexical_cast<time_t>(res);
-    }
-    if (fieldValue(data, "sync", "up_profile_id", res)){
+    if (fieldValue(data, "sync", "up_profile_id", res))
         _settings._upProfileId = boost::lexical_cast<unsigned>(res);
         _settings._upProfileId = boost::lexical_cast<unsigned>(res);
-    }
-    if (fieldValue(data, "sync", "down_profile_id", res)){
+    if (fieldValue(data, "sync", "down_profile_id", res))
         _settings._downProfileId = boost::lexical_cast<unsigned>(res);
         _settings._downProfileId = boost::lexical_cast<unsigned>(res);
-    }
-    if (fieldValue(data, "sync", "max_acl_per_pdu", res)){
+    if (fieldValue(data, "sync", "max_acl_per_pdu", res))
         _settings._maxACLPerPDU = boost::lexical_cast<size_t>(res);
         _settings._maxACLPerPDU = boost::lexical_cast<size_t>(res);
-    }
-    if (fieldValue(data, "sync", "data_url", res)){
+    if (fieldValue(data, "sync", "data_url", res))
         _settings._dataURL = res;
         _settings._dataURL = res;
-    }
+    if (fieldValue(data, "sync", "script_base", res))
+        _settings._scriptBase = res;
+    _settings._dumpScripts = fieldExists(data, "sync", "dump_scripts");
 }
 }
index 444324b946edeb84179eb67a9a6f02fc06e75ec7..71917ed798df5e6e0a519bfe576de63ad9a5000e 100644 (file)
@@ -130,21 +130,32 @@ void Switch::sync()
     }
 
     {
     }
 
     {
-        std::string fileName(_ip + ".sh");
+        std::ostream * stream = NULL;
+        std::string fileName(_settings.scriptBase() + "/" + _ip + ".sh");
         std::string newFileName(fileName + ".new");
         std::string newFileName(fileName + ".new");
-        std::ofstream script(newFileName.c_str());
-        script << "#!/bin/sh\n";
-        if (!dropACLs(target, script)) {
+
+        if (_settings.dumpScripts()) {
+            stream = new std::ofstream(newFileName.c_str());
+            *stream << "#!/bin/sh\n";
+        } else {
+            stream = new std::stringstream;
+        }
+
+        if (!dropACLs(target, *stream)) {
             logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
             logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
+            delete stream;
             return;
         }
 
             return;
         }
 
-        if (!createACLs(target, script)) {
+        if (!createACLs(target, *stream)) {
             logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl;
             logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl;
+            delete stream;
             return;
         }
             return;
         }
-        script.close();
-        rename(newFileName.c_str(), fileName.c_str());
+
+        delete stream;
+        if (_settings.dumpScripts())
+            rename(newFileName.c_str(), fileName.c_str());
     }
 
     if (_settings.isDebug()) {
     }
 
     if (_settings.isDebug()) {
@@ -217,7 +228,8 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S
     size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1;
     for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) {
         Pdu pdu;
     size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1;
     for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) {
         Pdu pdu;
-        stream << "snmpset -v2c -cgts_community_w " << _ip;
+        if (_settings.dumpScripts())
+            stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip;
         for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) {
             int id;
             if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) {
         for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) {
             int id;
             if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) {
@@ -230,9 +242,11 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S
             Vb vb(Oid(dropACLOid.c_str()));
             vb.set_value(int(6));
             pdu += vb;
             Vb vb(Oid(dropACLOid.c_str()));
             vb.set_value(int(6));
             pdu += vb;
-            stream << " " << dropACLOid << " i 6";
+            if (_settings.dumpScripts())
+                stream << " " << dropACLOid << " i 6";
         }
         }
-        stream << "\n";
+        if (_settings.dumpScripts())
+            stream << "\n";
         if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
             if (c != SNMP_ERROR_TOO_BIG) {
                 logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
         if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
             if (c != SNMP_ERROR_TOO_BIG) {
                 logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
@@ -260,7 +274,8 @@ bool Switch::createACLs(const CTarget & target, std::ostream & stream)
         pdu.clear();
         _aclsCreated = true;
         ++pos;
         pdu.clear();
         _aclsCreated = true;
         ++pos;
-        stream << "snmpset -v2c -cgts_community_w " << _ip << " " << *it << "\n";
+        if (_settings.dumpScripts())
+            stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip << " " << *it << "\n";
     }
     return true;
 }
     }
     return true;
 }