Exemple #1
0
        public List <Drive> GetDrives(IIdoit idoit)
        {
            if (hardDisks == null)
            {
                GetStorageDevices();
            }
            IdoitEnumerator fsEnumerator = idoit.Dialog(new Drive().Constant, "filesystem");
            //IdoitEnumerator onDeviceEnumerator = idoit.Dialog(new Drive().Constant, "device");    //Das funktioniert wegen einem Bug in der i-doit API leider nicht...

            List <Drive> result = new List <Drive>();

            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                if (!di.IsReady)
                {
                    continue;              //Wechseldatenträger und DVDs überspringen.
                }
                Drive child = new Drive();
                child.mount_point  = di.Name;
                child.title        = di.VolumeLabel;
                child.system_drive =
                    Environment.GetEnvironmentVariable("windir")
                    .ToLowerInvariant()
                    .StartsWith(di.RootDirectory.FullName.ToLowerInvariant())
                        ? 1
                        : 0;
                if (fsEnumerator.TestTitle(di.DriveFormat))
                {
                    child.filesystem = fsEnumerator.FindTitleLike(di.DriveFormat);
                }
                else
                {
                    idoit.DialogEdit(child, "filesystem", di.DriveFormat);
                    fsEnumerator     = idoit.Dialog(new Drive().Constant, "filesystem");
                    child.filesystem = fsEnumerator.FindTitleLike(di.DriveFormat);
                }
                child.capacity = (float)((long)(di.TotalSize / 1000 / 1000 / 1000));
                child.serial   = GetVolumeSerialNumber(di.Name);
                child.unit     = Drive.Unit.GB;
                //child.device lässt sich wegen o.g. Bug nicht korrekt setzen...
                result.Add(child);
            }
            return(result);
        }
Exemple #2
0
        public List <SoundCard> GetSoundCards(IIdoit idoit)
        {
            IdoitEnumerator  manufacEnum = idoit.Dialog(new SoundCard().Constant, "manufacturer");
            List <SoundCard> result      = new List <SoundCard>();

            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_SoundDevice");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    SoundCard child = new SoundCard();

                    //Wert für Hersteller herausfinden.
                    string manufacQuery = (string)queryObj["Manufacturer"];
                    if (!string.IsNullOrEmpty(manufacQuery))
                    {
                        if (!manufacEnum.TestTitle(manufacQuery))
                        {
                            //Hersteller hinzufügen, falls noch nicht bekannt...
                            idoit.DialogEdit(new SoundCard(), "manufacturer", manufacQuery);
                            manufacEnum = idoit.Dialog(new SoundCard().Constant, "manufacturer");
                        }
                        child.manufacturer = manufacEnum.FindTitleLike(manufacQuery);
                    }
                    child.title       = (string)queryObj["Caption"];
                    child.description = (string)queryObj["Description"];
                    result.Add(child);
                }
            }
            catch (Exception e)
            {
                throw;
            }
            return(result);
        }
Exemple #3
0
        private static void ScanLocal(string apiKey, string url, string username, string password)
        {
            //In i-doit einloggen, und Konstanten abfragen.
            Idoit idoit = null;

            try
            {
                idoit = Idoit.Login(apiKey, url, username, password);
            }
            catch (JsonRpcException rpcException)
            {
                Console.WriteLine("-> Unable to connect to i-doit:");
                Console.WriteLine(rpcException.Error.message);
                return;
            }
            catch (WebException we)
            {
                Console.WriteLine("-> Unable to connect to i-doit:");
                Console.WriteLine(string.Format("-> {1}", null, we.Message));
                return;
            }

            VersionResponse vr            = idoit.Version();
            Version         parsedVersion = new Version(vr.version);

            if (parsedVersion < new Version(1, 4, 8))
            {
                Console.WriteLine("-> This Program requires i-doit 1.4.8");
                idoit.Logout();
                return;
            }

            ConstantsResponse   constants = idoit.Constants();
            ObjectTypesResponse ot        = idoit.ObjectTypes();

            //Die i-doit API erlaubt es uns nicht Objekttypen programmatisch anzulegen oder zu modifizieren.
            //Von daher testen die drei nächsten Absätze, ob es möglich ist Notebooks einzutragen, falls nicht, wird der Anwender aufgefordert,
            //den Objekttyp anzulegen.
            if (!constants.objectTypes.ContainsValue("Notebook"))
            {
                Console.WriteLine("-> Could not find object type \"Notebook\" in your i-doit setup!");
                Console.WriteLine(
                    "-> Please create it by creating it in the Object type configuration in the i-doit web interface.");
                idoit.Logout();
                return;
            }
            ObjectTypeCategoriesResponse notebookCats = null;

            foreach (var m in constants.objectTypes)
            {
                if (m.Value.Equals("Notebook"))
                {
                    notebookCats = idoit.ObjectTypeCategories(m.Key);
                    break;
                }
            }
            foreach (Category c in new Category[] { new Drive(), new Model(), new SoundCard(), new Cpu(), new Memory(), new SoftwareAssignment(), })
            {
                if (!notebookCats.TestForConstant(c.Constant))
                {
                    Console.WriteLine("-> Could not find category \"{0}\" in object type \"Notebook\" in your i-doit setup!", c.DisplayName);
                    Console.WriteLine(
                        "-> Please add it by editing the Object type configuration in the i-doit web interface.");
                    idoit.Logout();
                    return;
                }
            }

            //Rauskriegen, auf was für einem OS wir laufen
            IOperatingSystemHandler osHandler;

            osHandler = DetectOperatingSystem();
            if (osHandler == null)
            {
                idoit.Logout();
                return;
            }

            //Herausfinden was wir für eine Art von PC sind...
            ObjectType targetObjectType = null;

            if (osHandler.IsLaptop())
            {
                //Ich gehe mal davon aus, das Notebooks nicht als Server-artige Geräte verwendet werden.
                targetObjectType = ot.GetObjectTypeFromTitle("Notebook");
            }
            else if (osHandler.IsVirtualMachine())
            {
                if (osHandler.IsServer())
                {
                    targetObjectType = ot.GetObjectTypeFromTitle("Virtual server");
                }
                else
                {
                    targetObjectType = ot.GetObjectTypeFromTitle("Virtual client");
                }
            }
            else
            {
                if (osHandler.IsVirtualMachineHost())
                {
                    targetObjectType = ot.GetObjectTypeFromTitle("Virtual host");
                }
                else if (osHandler.IsServer())
                {
                    targetObjectType = ot.GetObjectTypeFromTitle("Server");
                }
                else
                {
                    targetObjectType = ot.GetObjectTypeFromTitle("Desktop");
                }
            }

            ObjectTypeCategoriesResponse targetObjectTypeCategories = idoit.ObjectTypeCategories(targetObjectType);

            //Unseren Rechner aus der Datenbank rausfischen, oder ihn eintragen...
            CmdbObject        objectId;
            List <CmdbObject> o = idoit.FindObjects(Environment.MachineName, targetObjectType);

            if (o.Count > 1)
            {
                Console.WriteLine(
                    "Es gibt mehr als einen {0} der {1} heißt - bitte physikalisch beheben, dann in i-doit manuell löschen!");
                idoit.Logout();
                return;
            }
            else if (o.Count == 0)
            {
                CreateObjectResponse createObjectResponse = idoit.CreateCmdbObject(targetObjectType,
                                                                                   Environment.MachineName);
                if (!createObjectResponse.success)
                {
                    Console.WriteLine("-> {0}", createObjectResponse.message);
                    Console.WriteLine("-> Konnte das Objekt nicht anlegen, bitte in den Logs von i-doit nachsehen...");
                    idoit.Logout();
                    return;
                }
                objectId    = new CmdbObject();
                objectId.id = createObjectResponse.id;
            }
            else
            {
                objectId = o[0];
            }


            //CPU Hersteller
            IdoitEnumerator cpuManufactorEnumerator        = idoit.Dialog(new Cpu().Constant, "manufacturer");
            bool            cpuManufactorEnumeratorChanged = false;

            foreach (string manufac in Config.cpuManufactors)    //Liste der CPU Hersteller von: https://de.wikipedia.org/wiki/CPUID
            {
                if (!cpuManufactorEnumerator.TestTitle(manufac))
                {
                    idoit.DialogEdit(new Cpu(), "manufacturer", manufac);
                    cpuManufactorEnumeratorChanged = true;
                }
            }
            if (cpuManufactorEnumeratorChanged)
            {
                cpuManufactorEnumerator = idoit.Dialog(new Cpu().Constant, "manufacturer");
            }

            IdoitEnumerator cpuModelEnumator          = idoit.Dialog(new Cpu().Constant, "type");
            bool            cpuModelEnumeratorChanged = false;

            foreach (string model in Config.cpuModels)   //TODO: mehr Modell IDs rauskriegen...
            {
                if (!cpuModelEnumator.TestTitle(model))
                {
                    idoit.DialogEdit(new Cpu(), "type", model);
                    cpuModelEnumeratorChanged = true;
                }
            }
            if (cpuModelEnumeratorChanged)
            {
                cpuModelEnumator = idoit.Dialog(new Cpu().Constant, "type");
            }



            //CPU
            if (targetObjectTypeCategories.TestForConstant(new Cpu()))
            {
                List <Cpu> cpus = new List <Cpu>();
                cpus.AddRange(osHandler.GetCpu());
                foreach (Cpu c in cpus)
                {
                    string modelCmp = c.title.Replace("(TM)", "");
                    foreach (string manufac in Config.cpuManufactors)
                    {
                        if (c.cpuid.Contains(manufac))
                        {
                            c.manufacturer = cpuManufactorEnumerator.FindTitleLike(manufac);
                            break;
                        }
                    }
                    foreach (EnumInfo ei in cpuModelEnumator)
                    {
                        if (modelCmp.Contains(ei.title))
                        {
                            c.type = ei.id;
                            break;
                        }
                    }
                }
                List <Cpu> cpusInDb = idoit.CategoryRead <Cpu>(objectId.id, cpus[0]);
                idoit.SyncList(objectId, cpusInDb, cpus);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "CPU");
            }

            //Physikalische Laufwerke
            if (targetObjectTypeCategories.TestForConstant(new StorageDevice()))
            {
                List <StorageDevice> storageDevices     = osHandler.GetStorageDevices();
                List <StorageDevice> storageDevicesInDb = idoit.CategoryRead <StorageDevice>(objectId.id,
                                                                                             new StorageDevice());
                idoit.SyncList(objectId, storageDevicesInDb, storageDevices);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Storage Device");
            }

            //Logische Laufwerke
            if (targetObjectTypeCategories.TestForConstant(new Drive()))
            {
                List <Drive> drives     = osHandler.GetDrives(idoit);
                List <Drive> drivesInDb = idoit.CategoryRead <Drive>(objectId.id, new Drive());
                idoit.SyncList(objectId, drivesInDb, drives);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Drive");
            }

            //RAM Hersteller
            IdoitEnumerator ramManufactorEnumerator = idoit.Dialog(new Memory().Constant, "manufacturer");

            foreach (KeyValuePair <string, string> kv in Config.ramManufactors)
            {
                if (!ramManufactorEnumerator.TestTitle(kv.Value))
                {
                    idoit.DialogEdit(new Memory(), "manufacturer", kv.Value);
                    ramManufactorEnumerator = idoit.Dialog(new Memory().Constant, "manufacturer");
                }
            }

            //RAMs
            if (targetObjectTypeCategories.TestForConstant(new Memory()))
            {
                List <Memory> rams     = osHandler.GetMemoryBanks(idoit);
                List <Memory> ramsInDb = idoit.CategoryRead <Memory>(objectId.id, new Memory());
                idoit.SyncList(objectId, ramsInDb, rams);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Memory");
            }

            //Als nächstes wollen wir Software Assignments eintragen, dazu stellen wir zunächst sicher, dass jede installierte Software auch in i-doit bekannt ist.
            if (targetObjectTypeCategories.TestForConstant(new SoftwareAssignment()))
            {
                ObjectType          softwareObjectType    = ot.GetObjectTypeFromTitle("Application");
                List <SoftwareInfo> softwares             = osHandler.GetSoftware();
                List <CmdbObject>   softwareKnown         = idoit.FindObjects(softwareObjectType);
                IdoitEnumerator     softwareManufacturers = idoit.Dialog(new Applications().Constant, "manufacturer");
                foreach (SoftwareInfo si in softwares)
                {
                    if (softwareKnown.Find(x => x.title.Equals(si.productName)) == null)
                    {
                        //Software in i-doit bekannt machen.
                        int          newSoftwareId           = idoit.CreateCmdbObject(softwareObjectType, si.productName).id;
                        CmdbObject   newSoftware             = idoit.GetCmdbObject(newSoftwareId);
                        Applications newSoftwareApplications = new Applications();
                        newSoftwareApplications.install_path = si.installPath;
                        newSoftwareApplications.release      = si.version;

                        //Software-Entwickler in i-doit bekannt machen.
                        if (!string.IsNullOrEmpty(si.manufacterer))
                        {
                            bool manufacturerKnown = softwareManufacturers.TestTitle(si.manufacterer);
                            if (!manufacturerKnown)
                            {
                                idoit.DialogEditSpecific(newSoftwareApplications, "manufacturer", si.manufacterer);
                                softwareManufacturers = idoit.Dialog(new Applications().Constant, "manufacturer");
                            }
                            newSoftwareApplications.manufacturer = softwareManufacturers.FindTitleLike(si.manufacterer);
                        }

                        //Software-Info schreiben.
                        idoit.CategoryCreate(newSoftware, newSoftwareApplications);

                        softwareKnown = idoit.FindObjects(softwareObjectType);
                        //direkt nochmal abfragen, um Doubletten auszuschließen.
                    }
                }

                //Liste von Software Assignments erzeugen:
                List <SoftwareAssignment> assignments =
                    softwares.ConvertAll(
                        x => new SoftwareAssignment(softwareKnown.Find(y => y.title.Equals(x.productName)).id));

                //Das Betriebssystem ist nach der Logik von i-doit auch ein Software Assignment. Von daher müssen wir uns nun um Betriebssystem-Infos kümmern:
                string            uname = osHandler.uname();
                ObjectType        operatingSystemObjectType = ot.GetObjectTypeFromTitle("Operating System");
                List <CmdbObject> knownOperatingSystems     = idoit.FindObjects(operatingSystemObjectType);
                CmdbObject        operatingSystemCmdbObject = null;
                operatingSystemCmdbObject = knownOperatingSystems.Find((x) => x.title.Equals(uname));
                if (operatingSystemCmdbObject == null)
                {
                    operatingSystemCmdbObject = new CmdbObject(idoit.CreateCmdbObject(operatingSystemObjectType, uname));
                }
                SoftwareAssignment osSoftwareAssignment = new SoftwareAssignment();
                osSoftwareAssignment.application = operatingSystemCmdbObject.id;

                //Dem Software Assignment für das Betriebssystem ggf. eine Lizenz nahe bringen.
                if (osHandler.OperatingSystemNeedsLicense())    //sowas braucht bekanntlich nur Windows... :D
                {
                    string cdKey = osHandler.OperatingSystemLicensingInfo();
                    if (!string.IsNullOrEmpty(cdKey))
                    {
                        string licenseName = string.Format("{0}-Lizenz für {1}", uname, Environment.MachineName);

                        ObjectType        licenseObjectType = ot.GetObjectTypeFromTitle("Licenses");
                        List <CmdbObject> knownLicenses     = idoit.FindObjects(licenseObjectType);
                        CmdbObject        licenseCmdbObject = null;
                        operatingSystemCmdbObject = knownLicenses.Find((x) => x.title.Equals(licenseName));
                        if (operatingSystemCmdbObject == null)
                        {
                            //Neues Objekt für die Lizenz anlegen:
                            licenseCmdbObject = new CmdbObject(idoit.CreateCmdbObject(licenseObjectType, licenseName));
                            //...und die Lizenz im Feld "Description" hinterlegen.
                            Global licenseGlobal = idoit.CategoryRead <Global>(licenseCmdbObject.id, new Global())[0];
                            licenseGlobal.description = cdKey;

                            try
                            {
                                idoit.CategoryCreate <Global>(licenseCmdbObject,
                                                              licenseGlobal); //Gut zu wissen: Wenn eine Kategorie nur einmal in einem Objekt vorhanden ist, wird

                                //sie durch Create überschrieben...
                            }
                            catch (JsonRpcException rpcException)
                            {
                                Console.WriteLine(rpcException.Error.ToString());
                            }

                            try
                            {
                                idoit.CategoryUpdate(licenseCmdbObject,
                                                     licenseGlobal); //Gut zu wissen: Wenn eine Kategorie nur einmal in einem Objekt vorhanden ist, wird

                                //sie durch Create überschrieben...
                            }
                            catch (JsonRpcException rpcException)
                            {
                                Console.WriteLine(rpcException.Error.data.ToString());
                            }
                        }
                    }
                    //osSoftwareAssignment.assigned_license = licenseCmdbObject.id;     //kann nicht direkt zugewiesen werden, gibt eine Exception dass irgendwelche SQL-Constraints nicht erfüllt sind...

                    //TODO: Das selbe hier für Office und Konsorten machen...
                }
                assignments.Add(osSoftwareAssignment);

                List <SoftwareAssignment> assignmentsInDb = idoit.CategoryRead <SoftwareAssignment>(objectId.id,
                                                                                                    new SoftwareAssignment());
                idoit.SyncList(objectId, assignmentsInDb, assignments);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Software assignment");
            }

            //Samba Shares abgleichen
            if (targetObjectTypeCategories.TestForConstant(new Share()))
            {
                List <Share> shares     = osHandler.GetShares();
                List <Share> sharesInDb = idoit.CategoryRead <Share>(objectId.id, new Share());
                idoit.SyncList(objectId, sharesInDb, shares);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Share");
            }

            //Soundkarten abgleichen
            if (targetObjectTypeCategories.TestForConstant(new SoundCard()))
            {
                List <SoundCard> soundCards     = osHandler.GetSoundCards(idoit);
                List <SoundCard> soundCardsInDb = osHandler.GetSoundCards(idoit);
                idoit.SyncList(objectId, soundCardsInDb, soundCards);
            }
            else
            {
                Console.WriteLine("-> According to i-doit {0}s do not have {1}s", targetObjectType.title, "Sound card");
            }

            idoit.Logout();
        }
Exemple #4
0
        public List <Memory> GetMemoryBanks(IIdoit idoit)
        {
            List <Memory> result          = new List <Memory>();
            List <string> knownBankLabels = new List <string>();

            IdoitEnumerator titles      = idoit.Dialog(new Memory().Constant, "title");
            IdoitEnumerator manufactors = idoit.Dialog(new Memory().Constant, "manufacturer");
            IdoitEnumerator type        = idoit.Dialog(new Memory().Constant, "type");
            int             bankNo      = 0;

            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_PhysicalMemory");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    bankNo++;

                    string actualPartNumber = (string)queryObj["PartNumber"];                                       //ältere RAM Bänke geben evtl. null zurück
                    if (!string.IsNullOrEmpty(actualPartNumber))
                    {
                        actualPartNumber = actualPartNumber.Trim();
                    }

                    string actualManufacturer = (string)queryObj["Manufacturer"];                                   //ältere RAM Bänke geben evtl. null zurück
                    if (!string.IsNullOrEmpty(actualManufacturer))
                    {
                        actualManufacturer.Trim();
                    }

                    if (IsRamManufacturerSpecified(actualManufacturer))
                    {
                        //Wenn kein Hersteller definiert, dann anhand der PartNumber den Hersteller herausfinden.
                        if (!string.IsNullOrEmpty(actualPartNumber))
                        {
                            foreach (var kv in Config.ramManufactors)
                            {
                                if (actualPartNumber.StartsWith(kv.Key))
                                {
                                    actualManufacturer = kv.Value;
                                    break;
                                }
                            }
                        }
                    }

                    Memory child = new Memory();
                    child.quantity = 1;
                    string bankLabel = queryObj["BankLabel"].ToString();
                    if (knownBankLabels.Contains(bankLabel))
                    {
                        bankLabel = "Unknown Memory Bank " + bankNo;
                    }
                    else
                    {
                        knownBankLabels.Add(bankLabel);
                    }
                    if (!titles.TestTitle(bankLabel))
                    {
                        idoit.DialogEdit(new Memory(), "title", bankLabel);
                        titles = idoit.Dialog(new Memory().Constant, "title");
                    }
                    child.title = titles.FindTitleLike(bankLabel);

                    if (IsRamManufacturerSpecified(actualManufacturer))
                    {
                        if (!manufactors.TestTitle(actualManufacturer))
                        {
                            //Sollte der Hersteller des RAMs bekannt sein, und er ist weder in u-doit noch in i-doit bekannt, den RAM-Hersteller eintragen:
                            idoit.DialogEdit(new Memory(), "manufacturer", actualManufacturer);
                            manufactors = idoit.Dialog(new Memory().Constant, "manufacturer");
                        }

                        child.manufacturer = manufactors.FindTitleLike(actualManufacturer);
                    }

                    if (!string.IsNullOrEmpty(actualPartNumber))
                    {
                        if (!type.TestTitle(actualPartNumber))
                        {
                            idoit.DialogEdit(new Memory(), "type", actualPartNumber);
                            type = idoit.Dialog(new Memory().Constant, "type");
                        }
                        child.type = type.FindTitleLike(actualPartNumber);
                    }

                    child.capacity    = (float)(Convert.ToInt64(queryObj["Capacity"].ToString()) / 1024 / 1024);
                    child.unit        = Memory.C__CATG__MEMORY_UNIT.C__MEMORY_UNIT__MB;
                    child.description = queryObj["Description"].ToString();
                    result.Add(child);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(result);
        }