"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/core/hwcomponent.cpp" (23 Feb 2021, 7932 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hwcomponent.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2019 Raden Solutions
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: swpkg.cpp
   20 **
   21 **/
   22 
   23 #include "nxcore.h"
   24 
   25 /**
   26  * Comparator for hardware components
   27  */
   28 int HardwareComponentComparator(const HardwareComponent **c1, const HardwareComponent **c2)
   29 {
   30    int rc = static_cast<int>((*c1)->getCategory()) - static_cast<int>((*c2)->getCategory());
   31    if (rc != 0)
   32       return rc;
   33    if ((*c1)->getIndex() != (*c2)->getIndex())
   34       return (*c1)->getIndex() < (*c2)->getIndex() ? -1 : 1;
   35    rc = _tcscmp((*c1)->getSerialNumber(), (*c2)->getSerialNumber());
   36    if (rc != 0)
   37       return rc;
   38    rc = _tcscmp((*c1)->getPartNumber(), (*c2)->getPartNumber());
   39    if (rc != 0)
   40       return rc;
   41    rc = _tcscmp((*c1)->getModel(), (*c2)->getModel());
   42    if (rc != 0)
   43       return rc;
   44    rc = _tcscmp((*c1)->getLocation(), (*c2)->getLocation());
   45    if (rc != 0)
   46       return rc;
   47    return _tcscmp((*c1)->getVendor(), (*c2)->getVendor());
   48 }
   49 
   50 /**
   51  * Calculate hardware changes
   52  */
   53 ObjectArray<HardwareComponent> *CalculateHardwareChanges(ObjectArray<HardwareComponent> *oldSet, ObjectArray<HardwareComponent> *newSet)
   54 {
   55    HardwareComponent *nc = NULL, *oc = NULL;
   56    int i;
   57    ObjectArray<HardwareComponent> *changes = new ObjectArray<HardwareComponent>(16, 16);
   58 
   59    for(i = 0; i < newSet->size(); i++)
   60    {
   61       nc = newSet->get(i);
   62       oc = oldSet->find(nc, HardwareComponentComparator);
   63       if (oc == NULL)
   64       {
   65          nc->setChangeCode(CHANGE_ADDED);
   66          changes->add(nc);
   67       }
   68    }
   69 
   70    for(i = 0; i < oldSet->size(); i++)
   71    {
   72       oc = oldSet->get(i);
   73       nc = newSet->find(oc, HardwareComponentComparator);
   74       if (nc == NULL)
   75       {
   76          oc->setChangeCode(CHANGE_REMOVED);
   77          changes->add(oc);
   78       }
   79    }
   80 
   81    return changes;
   82 }
   83 
   84 /**
   85  * Create hardware component
   86  */
   87 HardwareComponent::HardwareComponent(HardwareComponentCategory category, UINT32 index, const TCHAR *type,
   88          const TCHAR *vendor, const TCHAR *model, const TCHAR *partNumber, const TCHAR *serialNumber)
   89 {
   90    m_category = category;
   91    m_index = index;
   92    m_type = MemCopyString(type);
   93    m_vendor = MemCopyString(vendor);
   94    m_model = MemCopyString(model);
   95    m_location = NULL;
   96    m_capacity = 0;
   97    m_partNumber = MemCopyString(partNumber);
   98    m_serialNumber = MemCopyString(serialNumber);
   99    m_description = NULL;
  100    m_changeCode = CHANGE_NONE;
  101 }
  102 
  103 /**
  104  * Create hardware component from database
  105  * Expected field order: category,component_index,hw_type,vendor,model,location,capacity,part_number,serial_number,description
  106  */
  107 HardwareComponent::HardwareComponent(DB_RESULT result, int row)
  108 {
  109    m_category = static_cast<HardwareComponentCategory>(DBGetFieldLong(result, row, 0));
  110    m_index = DBGetFieldULong(result, row, 1);
  111    m_type = DBGetField(result, row, 2, NULL, 0);
  112    m_vendor = DBGetField(result, row, 3, NULL, 0);
  113    m_model = DBGetField(result, row, 4, NULL, 0);
  114    m_location = DBGetField(result, row, 5, NULL, 0);
  115    m_capacity = DBGetFieldUInt64(result, row, 6);
  116    m_partNumber = DBGetField(result, row, 7, NULL, 0);
  117    m_serialNumber = DBGetField(result, row, 8, NULL, 0);
  118    m_description = DBGetField(result, row, 9, NULL, 0);
  119    m_changeCode = CHANGE_NONE;
  120 }
  121 
  122 /**
  123  * Create hardware component from table row
  124  */
  125 HardwareComponent::HardwareComponent(HardwareComponentCategory category, const Table *table, int row)
  126 {
  127    m_category = category;
  128    m_index = 0;
  129    m_type = NULL;
  130    m_vendor = NULL;
  131    m_model = NULL;
  132    m_location = NULL;
  133    m_capacity = 0;
  134    m_partNumber = NULL;
  135    m_serialNumber = NULL;
  136    m_description = NULL;
  137 
  138    for(int i = 0; i < table->getNumColumns(); i++)
  139    {
  140       const TCHAR *cname = table->getColumnName(i);
  141       if (!_tcsicmp(cname, _T("HANDLE")) ||
  142           !_tcsicmp(cname, _T("NUMBER")) ||
  143           !_tcsicmp(cname, _T("INDEX")))
  144          m_index = table->getAsUInt(row, i);
  145       else if (!_tcsicmp(cname, _T("LOCATION")))
  146          m_location = MemCopyString(table->getAsString(row, i));
  147       else if (!_tcsicmp(cname, _T("MANUFACTURER")))
  148          m_vendor = MemCopyString(table->getAsString(row, i));
  149       else if (!_tcsicmp(table->getColumnName(i), _T("NAME")) ||
  150                !_tcsicmp(table->getColumnName(i), _T("VERSION")) ||
  151                !_tcsicmp(table->getColumnName(i), _T("FORM_FACTOR")) ||
  152                !_tcsicmp(table->getColumnName(i), _T("PRODUCT")))
  153          m_model = MemCopyString(table->getAsString(row, i));
  154       else if (!_tcsicmp(table->getColumnName(i), _T("PART_NUMBER")))
  155          m_partNumber = MemCopyString(table->getAsString(row, i));
  156       else if (!_tcsicmp(table->getColumnName(i), _T("CAPACITY")) ||
  157                !_tcsicmp(table->getColumnName(i), _T("SPEED")) ||
  158                !_tcsicmp(table->getColumnName(i), _T("CURR_SPEED")) ||
  159                !_tcsicmp(table->getColumnName(i), _T("SIZE")))
  160          m_capacity = table->getAsUInt64(row, i);
  161       else if (!_tcsicmp(table->getColumnName(i), _T("SERIAL_NUMBER")) ||
  162                !_tcsicmp(table->getColumnName(i), _T("MAC_ADDRESS")))
  163          m_serialNumber = MemCopyString(table->getAsString(row, i));
  164       else if ((!_tcsicmp(table->getColumnName(i), _T("TYPE")) && (category != HWC_STORAGE)) ||
  165                !_tcsicmp(table->getColumnName(i), _T("TYPE_DESCRIPTION")) ||
  166                !_tcsicmp(table->getColumnName(i), _T("CHEMISTRY")))
  167          m_type = MemCopyString(table->getAsString(row, i));
  168    }
  169 
  170    m_changeCode = CHANGE_NONE;
  171 }
  172 
  173 /**
  174  * Destructor
  175  */
  176 HardwareComponent::~HardwareComponent()
  177 {
  178    MemFree(m_type);
  179    MemFree(m_vendor);
  180    MemFree(m_model);
  181    MemFree(m_partNumber);
  182    MemFree(m_serialNumber);
  183    MemFree(m_location);
  184    MemFree(m_description);
  185 }
  186 
  187 /**
  188  * Fill NXCPMessage
  189  */
  190 void HardwareComponent::fillMessage(NXCPMessage *msg, UINT32 baseId) const
  191 {
  192    UINT32 fieldId = baseId;
  193    msg->setField(fieldId++, static_cast<INT16>(m_category));
  194    msg->setField(fieldId++, m_index);
  195    msg->setField(fieldId++, CHECK_NULL_EX(m_type));
  196    msg->setField(fieldId++, CHECK_NULL_EX(m_vendor));
  197    msg->setField(fieldId++, CHECK_NULL_EX(m_model));
  198    msg->setField(fieldId++, CHECK_NULL_EX(m_location));
  199    msg->setField(fieldId++, m_capacity);
  200    msg->setField(fieldId++, CHECK_NULL_EX(m_partNumber));
  201    msg->setField(fieldId++, CHECK_NULL_EX(m_serialNumber));
  202    msg->setField(fieldId++, CHECK_NULL_EX(m_description));
  203 }
  204 
  205 /**
  206  * Save to database
  207  * Field oerder: node_id,category,component_index,hw_type,vendor,model,location,capacity,part_number,serial_number,description
  208  */
  209 bool HardwareComponent::saveToDatabase(DB_STATEMENT hStmt) const
  210 {
  211    DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_category);
  212    DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, m_index);
  213    DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, m_type, DB_BIND_STATIC);
  214    DBBind(hStmt, 5, DB_SQLTYPE_VARCHAR, m_vendor, DB_BIND_STATIC);
  215    DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, m_model, DB_BIND_STATIC);
  216    DBBind(hStmt, 7, DB_SQLTYPE_VARCHAR, m_location, DB_BIND_STATIC);
  217    DBBind(hStmt, 8, DB_SQLTYPE_BIGINT, m_capacity);
  218    DBBind(hStmt, 9, DB_SQLTYPE_VARCHAR, m_partNumber, DB_BIND_STATIC);
  219    DBBind(hStmt, 10, DB_SQLTYPE_VARCHAR, m_serialNumber, DB_BIND_STATIC);
  220    DBBind(hStmt, 11, DB_SQLTYPE_VARCHAR, m_description, DB_BIND_STATIC);
  221    return DBExecute(hStmt);
  222 }