0

I am working on simple XML dashboard in Splunk to compute and display infra requirements for new Splunk environment. Below is an excerpt from the code which I am not able to get right:

    $(document).on('click', '#submitDemand', function(e) {
    e.preventDefault();
    //hostname, ip, storage, ram, os, component, cores
    indexers = indexernum($('[name="form_totalgb"]').val());
    console.log(indexers);
    shs = shnum($('[name="form_totalgb"]').val());
    console.log(shs);
    spinfraTableSearch = new SearchManager({
        id: "search2",
        search: "| inputlookup dm_splunk_infra_lookup| search demand_id=" + demand_id + " | table provisioned_hostname, provisioned_ip, splunk_component, cpu_cores, ram, storage_tb, os",
        preview: true,
        autostart: false,
        cache: true
    });
    spInfraCleanSearch = new SearchManager({
        id: "cleansearch",
        search: "| inputlookup dm_splunk_infra_lookup | eval key=_key | WHERE NOT demand_id=" + demand_id + " | outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    hf1search = new SearchManager({
        id: "hf1",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"HeavyForwarder1\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Windows\"| eval splunk_component = \"Heavy Forwarder\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    hf2search = new SearchManager({
        id: "hf2",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"HeavyForwarder2\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Heavy Forwarder\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    dssearch = new SearchManager({
        id: "ds",
        search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"DeploymentServer\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Deployment Server\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
        preview: true,
        autostart: false,
        cache: true
    });
    //spInfraTokens("DeploymentServer", "53.xx.xxx.xxx", "0.5 TB", "8 GB", "Linux", "Deployment Server", "8");

    if (indexers > 1) {
        cmsearch = new SearchManager({
            id: "cm",
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"ClusterMaster\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Cluster Master\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    for (i = 0; i < indexers; i++) {
        idxsearch[i] = new SearchManager({
            id: "idx" + i,
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"Indexer" + (i + 1) + "\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"" + ($('[name="form_totalstorage"]').val() / indexers) + " TB\"| eval ram = \"64 GB\"| eval os = \"Linux\"| eval splunk_component = \"Indexer\"| eval cpu_cores = \"24\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    //console.log(idxsearch);
    if (shs > 1) {
        depsearch = new SearchManager({
            id: "dep",
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"Deployer\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"8 GB\"| eval os = \"Linux\"| eval splunk_component = \"Deployer\"| eval cpu_cores = \"8\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    for (i = 0; i < shs; i++) {
        shssearch[i] = new SearchManager({
            id: "shs" + i,
            search: "| inputlookup dm_splunk_infra_lookup | append [stats count| eval demand_id =" + demand_id + "| eval provisioned_hostname =\"SearchHead" + (i + 1) + "\" | eval provisioned_ip = \"53.xx.xxx.xxx\"| eval storage_tb = \"0.5 TB\"| eval ram = \"32 GB\"| eval os = \"Linux\"| eval splunk_component = \"Search Head\"| eval cpu_cores = \"16\" ] | table demand_id, provisioned_hostname, provisioned_ip, storage_tb, ram, os, splunk_component, cpu_cores| outputlookup dm_splunk_infra_lookup",
            preview: true,
            autostart: false,
            cache: true
        });
    }
    spInfraCleanSearch.startSearch();
    spInfraCleanSearch.on('search:done', function() {
        console.log('cleanup completed');
        hf1search.startSearch();
        spInfraTokens();
    });
    hf1search.on('search:done', function() {
        console.log('hf1 completed');
        spinfraTableSearch.startSearch();
        hf2search.startSearch();
        spInfraTokens();
    });
    hf2search.on('search:done', function() {
        console.log('hf2 completed');
        spinfraTableSearch.startSearch();
        dssearch.startSearch();
        spInfraTokens();
    });
    dssearch.on('search:done', function() {
        console.log('ds completed');
        if (indexers > 1) {
            console.log('start cmsearch');
            spinfraTableSearch.startSearch();
            cmsearch.startSearch();
            cmsearch.on('search:done', function() {
                console.log('cm completed');
                console.log('start idxsearch[0]');
                spinfraTableSearch.startSearch();
                idxsearch[0].startSearch();
            });
        } else {
            console.log('start idxsearch[0]');
            spinfraTableSearch.startSearch();
            idxsearch[0].startSearch();
            idxsearch[0].on('search:done', function() {
                console.log('idx0 completed');
                console.log('start shssearch[0]');
                spinfraTableSearch.startSearch();
                shssearch[0].startSearch();
            });
        }
    });
    var i = 1;
    for (i = 1; i < indexers; i++) {
        console.log('in for register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start idxsearch[' + i + ']');
            spinfraTableSearch.startSearch();
            idxsearch[i].startSearch();
        });
    }
    if (shs > 1) {
        console.log('register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start depsearch');
            spinfraTableSearch.startSearch();
            depsearch.startSearch();
        });
        console.log('register deployer');
        depsearch.on('search:done', function() {
            console.log('deployer completed');
            console.log('start shssearch[0]');
            spinfraTableSearch.startSearch();
            shssearch[0].startSearch();
        });
    } else {
        console.log('register idx[' + (i - 1) + ']');
        idxsearch[i - 1].on('search:done', function() {
            console.log('idx' + (i - 1) + ' completed');
            console.log('start shssearch[0]');
            spinfraTableSearch.startSearch();
            shssearch[0].startSearch();
            console.log('register shs[0]');
            shssearch[0].on('search:done', function() {
                console.log('shs0 completed');
                spinfraTableSearch.startSearch();
                spInfraTokens();
            });
        });
    }
    var i = 1;
    for (i = 1; i < shs; i++) {
        console.log('register shs[' + (i - 1) + ']');
        shssearch[i - 1].on('search:done', function() {
            console.log('shs' + (i - 1) + ' completed');
            console.log('start shssearch[' + i + ']');
            spinfraTableSearch.startSearch();
            shssearch[i].startSearch();
        });
    }
    console.log('register shs[' + (i - 1) + ']');
    shssearch[i - 1].on('search:done', function() {
        console.log('shs' + (i - 1) + ' completed');
        spinfraTableSearch.startSearch();
        spInfraTokens();
    });

});
createSpInfra.on('search:done', function() {
    console.log('createspinfra completed');
    spinfraTableSearch.startSearch();
    tokens.unset('tok_sp_create');
});
spinfraTable.on('click', function(e) {
    e.preventDefault();
});

As you know, this function is called when submit is clicked and I have included console.log() to debug the code, please excuse the quick/dirty nature. For variables indexers and shs, values 3 and 2 produces below output:

3
2
in for register idx[0]
in for register idx[1]
register idx[2]
register deployer
register shs[0]
register shs[1]
cleanup completed
hf1 completed
hf2 completed
ds completed
start cmsearch
cm completed
start idxsearch[0]
idx1 completed
start idxsearch[2]
idx1 completed
start depsearch
deployer completed
start shssearch[0]
shs1 completed
start shssearch[2]
Uncaught TypeError: Cannot read property 'startSearch' of undefined
    at constructor.<anonymous> (crud_set_types.js:564:18)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:5040)
    at constructor.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at Backbone.Model.done (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:341:24540)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:4974)
    at Backbone.Model.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:383:6359
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:10608)
    at _.(anonymous function) [as each] (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:27948)
shs1 completed
start shssearch[2]
Uncaught TypeError: Cannot read property 'startSearch' of undefined
    at constructor.<anonymous> (crud_set_types.js:564:18)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:5040)
    at constructor.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at Backbone.Model.done (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:341:24540)
    at triggerEvents (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:4974)
    at Backbone.Model.trigger (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:29:3949)
    at http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:383:6359
    at Array.forEach (<anonymous>)
    at Function._.each._.forEach (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:10608)
    at _.(anonymous function) [as each] (http://130.211.253.134:8000/en-US/static/@CB1E2AFE6D77CD53A7CEF5DF0465A971197F39141EDB7D8858CA184912B21663.1/build/pages/enterprise/common.js:27:27948)
shs1 completed
start shssearch[2]
and shs1/shssearch[2] keeps on repeating 

I am not able to figure out why while executing indexer loop, start idxsearch[1] is not executed even though idx[0], idx[1] and idx[2] are registered.

Similarly in shs block, start shssearch[1] should be called not start shssearch[2]. Further the shs for loop should not execute if i=2 but here it becomes as infinite loop.

Any help in this matter is appreciated. I am stuck in this since weeks and have rewritten this at least 4 times.

Best Regards, Nikhil

Edit: Shorter version:

     var i = 1;
for (i = 1; i < indexers; i++) {
    console.log('in for register idx[' + (i - 1) + ']');
    idxsearch[i - 1].on('search:done', function() {
        console.log('idx' + (i - 1) + ' completed');
        console.log('start idxsearch[' + i + ']');
        spinfraTableSearch.startSearch();
        idxsearch[i].startSearch();
    });
}

Output: start idxsearch[0] idx1 completed start idxsearch[2] Issue: idx0 completed, start idxsearch[1] and idx1 completed are missing. Those piece of code should be executed

Code2:

    var i = 1;
for (i = 1; i < shs; i++) {
    console.log('register shs[' + (i - 1) + ']');
    shssearch[i - 1].on('search:done', function() {
        console.log('shs' + (i - 1) + ' completed');
        console.log('start shssearch[' + i + ']');
        spinfraTableSearch.startSearch();
        shssearch[i].startSearch();
    });

Issue: similar problem shs0 completed and start shssearch[1] are missing. And this is in infinite loop where as value of i as 2 should restrict it.

nikhil14
  • 11
  • 5
  • 1
    This is too much code, you should be able to reproduce your problem with a lot less code. Until you can do that, you should do your own debugging – Ruan Mendes Dec 28 '17 at 17:41
  • well, I agree but this is to provide a holistic view. The problem pieces are `for loop` for `indexers` and `shs`. Main issue that I cant understand is when I get good values when registering the hooks why does it skips while executing them? – nikhil14 Dec 28 '17 at 17:46
  • 1
    Sorry nikhil, this is more code than I'm willing to look at, for free at least :) You have to make it easy on those willing to help, do you own debugging, break down the problem, you may solve it yourself, SO is not a tool to keep you from debugging your code, it's for after you've debugged it and can't still get it to work. First step is remove chunks of code until the bug is gone... – Ruan Mendes Dec 28 '17 at 17:53
  • I understand, half of above code is setting the pretext for those two loops. I have edited the post to explain my earlier comment and highlighting prob areas. But still keeping the full code for someone who is willing to go through it and will be helpful for someone who is developing in Splunk. Please try to give it 10 mins of your time. Thanks – nikhil14 Dec 28 '17 at 18:09
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – James Dec 28 '17 at 18:17
  • wow, thanks James it does seems like the same issue, let me attempt to solve using the examples given. However, what would be the reason for correct values when registering hooks? – nikhil14 Dec 28 '17 at 18:29
  • Not really sure which part of your code is the registering of hooks, but I would guess that you are not using `i` within the inner, anonymous functions of that part of your code (at least in any important way, like beyond logging it). – James Dec 28 '17 at 18:38
  • 1
    Thanks James, it was JavaScript closure bug and I was not aware about the concept. Now code works as it should. How should I mark your comment as answer? – nikhil14 Dec 29 '17 at 09:15

0 Answers0