X-Git-Url: https://git.stg.codes/ssmd.git/blobdiff_plain/19b2c8a4cdc62abc4596e422865ad89a082fc3a1..HEAD:/src/switch.cpp diff --git a/src/switch.cpp b/src/switch.cpp index 1fb0d66..71917ed 100644 --- a/src/switch.cpp +++ b/src/switch.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include "snmp_pp/snmp_pp.h" @@ -10,8 +12,8 @@ #include "snmptable.h" #include "oids.h" -using GTS::Switch; -using GTS::SNMPTable; +using SSMD::Switch; +using SSMD::SNMPTable; Switch::Switch(const Settings & settings, Snmp & snmp, @@ -67,7 +69,7 @@ Switch::~Switch() return; } - if (!dropACLs(target)) { + if (!dropACLs(target, std::cerr)) { logger << "Switch::~Switch() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl; return; } @@ -127,14 +129,33 @@ void Switch::sync() return; } - if (!dropACLs(target)) { - logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl; - return; - } + { + std::ostream * stream = NULL; + std::string fileName(_settings.scriptBase() + "/" + _ip + ".sh"); + std::string newFileName(fileName + ".new"); - if (!createACLs(target)) { - logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl; - return; + 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; + delete stream; + return; + } + + if (!createACLs(target, *stream)) { + logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl; + delete stream; + return; + } + + delete stream; + if (_settings.dumpScripts()) + rename(newFileName.c_str(), fileName.c_str()); } if (_settings.isDebug()) { @@ -164,7 +185,7 @@ bool Switch::checkProfiles(const CTarget & target) return false; } -bool Switch::dropACLs(const CTarget & target) +bool Switch::dropACLs(const CTarget & target, std::ostream & stream) { std::string upOidValue(swACLEtherRuleAccessID); upOidValue += "."; @@ -183,13 +204,13 @@ bool Switch::dropACLs(const CTarget & target) return false; } if (!aclsUpTable.empty()) { - if (!dropACLsByTable(target, _settings.upProfileId(), aclsUpTable)) { + if (!dropACLsByTable(target, _settings.upProfileId(), aclsUpTable, stream)) { logger << "Switch::dropACLs() - failed to drop acls from upload table for the switch '" << _ip << "'" << std::endl; return false; } } if (!aclsDownTable.empty()) { - if (!dropACLsByTable(target, _settings.downProfileId(), aclsDownTable)) { + if (!dropACLsByTable(target, _settings.downProfileId(), aclsDownTable, stream)) { logger << "Switch::dropACLs() - failed to drop acls from download table for the switch '" << _ip << "'" << std::endl; return false; } @@ -197,7 +218,7 @@ bool Switch::dropACLs(const CTarget & target) return true; } -bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const SNMPTable & table) +bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const SNMPTable & table, std::ostream & stream) { std::string dropACLOidPrefix(swACLEtherRuleRowStatus); dropACLOidPrefix += "."; @@ -207,6 +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; + 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) { @@ -219,7 +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; + if (_settings.dumpScripts()) + stream << " " << dropACLOid << " i 6"; } + 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; @@ -230,7 +257,7 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S return true; } -bool Switch::createACLs(const CTarget & target) +bool Switch::createACLs(const CTarget & target, std::ostream & stream) { std::vector::const_iterator it; size_t pos = 0; @@ -247,6 +274,8 @@ bool Switch::createACLs(const CTarget & target) pdu.clear(); _aclsCreated = true; ++pos; + if (_settings.dumpScripts()) + stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip << " " << *it << "\n"; } return true; }